but I am struggling to open a folder using Perl.
Dear Monk,
First, excuse the professor in me, let me make a minor correction to the terminology you are using. What you refer to as a "folder" is actually a directory and Perl provides a wonderful function for reading directories. Here is some sample code to help you along your way:
#!/usr/bin/perl -w
use strict;
my $dirname="/path/to/the/directory";
opendir(DIR,$dirname) or die "$dirname:$!";
while (my $entry=readdir(DIR)){
next if $entry eq '.';
next if $entry eq '..';
next if -d $entry; # see notes below
| do something with this...
}
exit(0);
the
-d $entry is to prevent you from attempting to do something unintended with a subdirectory under the desired directory. After you get past all the next logic (which could have been combined in one statement) you then add your code to actually do something with the file names you would get as a result of the rest of the code.
If you want to recursively operate on that directory here is an example of that code well modified:
#!/usr/bin/perl -w
use strict;
workTheDirectory("/path/to/the/directory");
exit(0);
#
#
sub workTheDirectory {
my $dirname=shift;
opendir(DIR,$dirname) or die "$dirname:$!";
while (my $entry=readdir(DIR)){
next if $entry eq '.';
next if $entry eq '..';
if ( -d $entry ) {
my $newdir = $dirname . "/" . $entry; #grow the path
workTheDirectory($newdir);
}
| This is a file...
| work it.
}
return;
}
Clear as mud?
Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg