in reply to recursive sub
First the obligarory use File::Find. Here is how to do it without using recursion - it is presented as an example rather than a recommendation. Search is width first rather than depth first. It is interesting in that we add new dirs to the array we are currently looping over. This comes in handy for other things as well.
If you do a Super Search for 'walk the directory tree' and 'recursion' you will find lots of examples. You could also do a search for File::Find and find most of them :-)
#!/usr/bin/perl -w use strict; my $root = 'c:/cluster1/'; my $delim = '/'; 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) { # skip the dot files next if $file eq '.' or $file eq '..'; # skip symbolic links next if -l $path.$file; if ( -d $path.$file ) { # add the dir to our dir list push @dirs, $path.$file.$delim; } else { # add the file to file list push @files, $path.$file; } } closedir DIR; } print "Directory list\n\n"; print "$_\n" for sort @dirs; print "\n\nFile List\n\n"; print "$_\n" for sort @files;
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|
|---|