Chapter 16 – Perl Directory Handling

Reading directories and scanning for files may seem like a difficult task but in Perl, it’s actually quite trivial.

This tutorial is geared more towards CGI usage but it can just as easily be converted back into command-line Perl so you can scan your own system. You open directories much the same way as you learned to up files from an earlier tutorial.

opendir(FILEHANDLE, $directory);

When you open the directory, you’re actually opening and using the filehandle to access the directory findings. The $directory is the file path of the directory you wish to access. It could have been hard coded such as

opendir(FILEHANDLE, “/home/user/public_html/”);

One thing to remember whenever you open a file or directory is to flip on the warnings of or die to ensure that it opened successfully. By default, if a file or directory is not opened for whatever reason (whether it be bad permissions or the directory or file cannot be found), Perl will not give you an error. It will continue on and pretend it’s working.

opendir(FILEHANDLE, $directory) or die “Error occurred: $!”;

Once you have the directory opened we need to read from it using readdir. Since we’re actually working with the filehandle itself and not the directory, we use

opendir(FILEHANDLE, $directory) or die “Error occurred: $!”;
my @files = readdir(FILEHANDLE);

Then of course you should close the filehandle once you’re done using it. No sense to keep a filehandle open longer than it has to be and accidently expose it to some unnatural events.

opendir(FILEHANDLE, $directory) or die “Error occurred: $!”;
my @files = readdir(FILEHANDLE);
close(FILEHANDLE);

From here, all you’d need to do is print out the array @files to see everything that you found in your directory.  When reading the print out, not all files will have a file extension so-to-speak. Even though most of these are more than likely directory names, there are some files that just don’t have extensions.

opendir(FILEHANDLE, $directory) or die “Error occured: $!”;
my @files = readdir(FILEHANDLE);
close(FILEHANDLE);

foreach(@files)
{
  print “$_<br>”;
}

.
..
cb.shtml
cgi-bin
_vti_pvt
adboxes.shtml
.htaccess
_vti_log
_private
_vti_txt
images
_vti_cnf
_vti_bin
tutorials
scripts
demo
archives.shtml
contact.shtml
custom.shtml

If you want to check to see which are directories and which of the items contained in @files are actually files, we can check with -d. This will check to see if the tested item is a directory. Since your array @files only contains the filename at this point, you need to prepend the $directory back onto it. This is the reason we prefer to store the directory in a variable rather than having to type it in numerous times.

my $directory = “/home/user/public_html/”; opendir(FILEHANDLE, $directory) or die “Error occured: $!”;
my @files = readdir(FILEHANDLE);
close(FILEHANDLE);

foreach my $file (@files)
{
  if (-d “$directory/$file”)
  {
    print “Directory: $file<br>”;
  }
  else
  {
    print “File: $file<br>”;
  }
}

There are many other ways to manipulate your files or directories that you’ve scanned. Let’s say for example you wanted to only find the .txt files that are in your directory.  To do this, we’d just take a regex and a simple if conditional.

my $directory = “/home/spyders/public_html/”; opendir(FILEHANDLE, $directory) or die “Error occured: $!”;
my @files = readdir(FILEHANDLE);
close(FILEHANDLE);

foreach my $file (@files)
{
  if ($file =~ m/\.txt$/i)
  {
    print “Directory: $file<br>”;
  }
  }

create.txt
test.txt
sample.txt

This concludes this lessonon reading from a directory. There are many more ways to manipulate your results to fit your needs. The sky and your imagination are the only limits.

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)