Here is some code I wrote back in 2001 that takes advantage of the fact that we can push onto an array we are currently iterating over. So it's called an infinite stream eh, cute name. Anyway this happily recurses a directory (or any other) tree.
my $root = 'c:/somedir/';
my @dirs = ($root);
my @files;
for my $path (@dirs){
opendir ( DIR, $path ) or next; # skip dirs we can't read
while (my $file = readdir DIR) {
next if $file eq '.' or $file eq '..';# skip dot files
next if -l $path.$file; # skip sym links
if ( -d $path.$file ) {
push @dirs, $path.$file.'/'; # add dir to list
}
else {
push @files, $path.$file; # add file to list
}
}
closedir DIR;
}
|