# Last update: 2009/08/15 # # READ DIRECTORY (SUB) TREE # # (p) J. Nagasaki # # Reads all sub directories starting at a base directory and # stores all paths into @filepath # # global directory list @filepath; # Root directory $rootdir = "c:\\Progra~1\\Perl"; # call to recursive self calling sub routine getdir($rootdir); # show results foreach (@filepath) { print "$_\n"; } #-------------------------- # get directory (recursive called subroutine) # all variables have to be declared with "my" to give them their # own area of validity in the actual recursion level sub getdir { my $dir = shift; opendir(DIR, "$dir"); # UPDATE: Speed-up (Files w/o "."): my @files = grep {!/\./} readdir(DIR); my @files = readdir(DIR); closedir(DIR); foreach (@files) { my $newdir = "$dir\\$_"; # create absolute path # check if directory and not "." or ".." # UPDATE: Speed-up (Files w/o "."): if (-d $newdir) if ((-d $newdir) && ($_ ne "\.") && ($_ ne "\.\.")) { push (@filepath, $newdir); # add new directory getdir($newdir); # recursive call } } }