The following subroutine should take an input path (Dir0), process that directory by recursively calling itself on subdirectories or processing any files it contains. The problem I am experiencing is that in the following example file structure it process Dir1 correctly, but after it returns to process the next item in the Dir0, it improperly identifies Dir2 as a file, not a directory. I am sure there is probably a better way of doing this, however, I am somewhat of a newbie to Perl and would like to understand why this code isn't working as I am expecting it to.


Example File Structure

Dir0
   Dir1
      file11
      file12
   Dir2
   file1
   file2



sub ProcessDir() {
 my $cwd = getcwd();
 print "CWD1 = $cwd\n";

 my $curPath = @_[0];

 chdir($curPath);
 my $cwd = getcwd();

 print "CWD2 = $cwd\n";

 opendir (DIR, $cwd) or die "Unable to open: $cwd : $!\n";
 my @files = readdir DIR;
 closedir(DIR);

 foreach $file (@files) {
   $result = (-d $file);
   print "Result = $result\n";
   if ($result) {
     print "$file is a directory.\n";
     if ($file ne '.' && $file ne '..') {
       $newPath = $curPath.$file;
       print "New path: $newPath\n";
       ProcessDir($newPath);
     }
   }
   else {
     print "$file is a file.\n";
   }
 }
}


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to