P
Patrick H.
Hi, I am toying around with the directory operations chapter of
"Learning Perl" and ran into a bit of a snag.
I am trying to open a directory, output all the -f files to a @files
array and all the -d files to a @folders array. The problem is it
seems to only work for the first instance of readdir I use; they both
work individually when I comment the other out, but when I have them
both only the first array is populated. Do I need to closedir and then
opendir again, or is there a way for me to reset the cursor (if that
is even the right terminology)?
Below is my code, and advice would be appreciated.
Patrick H.
#!/usr/bin/perl -w
use strict;
# folder to scan
my $folder = 'c:/';
# open folder
opendir C_DRIVE, $folder or die "problem reading $folder: $!";
my @files = grep { -f "$folder/$_" } readdir(C_DRIVE); # all
files
my @folders = grep { -d "$folder/$_" } readdir(C_DRIVE); # all
folders
# close folder
closedir C_DRIVE;
# test results
print "your files: @files\n";
print "your folders: @folders\n";
"Learning Perl" and ran into a bit of a snag.
I am trying to open a directory, output all the -f files to a @files
array and all the -d files to a @folders array. The problem is it
seems to only work for the first instance of readdir I use; they both
work individually when I comment the other out, but when I have them
both only the first array is populated. Do I need to closedir and then
opendir again, or is there a way for me to reset the cursor (if that
is even the right terminology)?
Below is my code, and advice would be appreciated.
Patrick H.
#!/usr/bin/perl -w
use strict;
# folder to scan
my $folder = 'c:/';
# open folder
opendir C_DRIVE, $folder or die "problem reading $folder: $!";
my @files = grep { -f "$folder/$_" } readdir(C_DRIVE); # all
files
my @folders = grep { -d "$folder/$_" } readdir(C_DRIVE); # all
folders
# close folder
closedir C_DRIVE;
# test results
print "your files: @files\n";
print "your folders: @folders\n";