readdir() attempted on invalid dirhandle at C:/Perl/site/lib/File/DirWalk.pm line 100. closedir() attempted on invalid dirhandle at C:/Perl/site/lib/File/DirWalk.pm line 117. #### opendir my $dirh, $path || return FAILED; my @dir_contents = readdir $dirh; @dir_contents = File::Spec->no_upwards(@dir_contents); foreach my $f (@dir_contents) { # be portable. my @dirs = File::Spec->splitdir($path); my $path = File::Spec->catfile(@dirs, $f); my $r = $self->walk($path); if ($r == PRUNE) { last; } elsif ($r != SUCCESS) { return $r; } } closedir $dirh; #### #!/usr/bin/perl use strict; use warnings; ##### # TreeWalker.pl # This script walks a directory tree starting at the directory given # on the command line (uses the current directory if none given). # It keeps track of the number and total size of: # Files # Files whose name ends in ~ # Files whose name ends in .pl # Files whose name ends in .txt # Directories # # and reports these numbers at the end of the script. ##### use Path::Class qw(file dir); use File::DirWalk; my $fileN = 0; # Counter for number of files my $fileSize = 0; # Total size of files my $backupN = 0; # Counter for number of backup files my $backupSize = 0; # Total size of backup files my $txtN = 0; # Counter for number of text files my $txtSize = 0; # Total size of text files my $plN = 0; # Counter for number of PERL scripts my $plSize = 0; # Total size of PERL scripts my $dirN = 0; # Counter for number of directories my $dirSize = 0; # Total size of directories ##### # d o F i l e # Process one file as the tree is walked ##### sub doFile { my $path = shift; my $file = file(split("/[\\\/]", $path)); # Make the path a 'file' my $stat = $file->stat(); # stat the file if (not defined $stat) { return; } # wierd error ... ignore file $fileN++; # Incr. # of files $fileSize += $stat->size; # Incr. file size my $base = $file->basename; # Get the base to check for special names if($base =~ m/~$/) { $backupN++; # backup files (end with ~) $backupSize += $stat->size; } elsif($base =~ m/\.txt$/) { $txtN++; # text files (end with .txt) $txtSize += $stat->size; } elsif($base =~ m/\.pl$/) { $plN++; # perl files (end with .pl) $plSize += $stat->size; } return File::DirWalk::SUCCESS; # return TRUE so walk continues } ##### # d o D i r # Process one directory as the tree is walked ##### sub doDir { my $path = shift; my $dir = dir(split(/[\\\/]/, $path)); # Make the path a 'dir' my $stat = $dir->stat(); # Stat the dir if (not defined $stat) { return; } # Wierd error ... ignore the dir $dirN++; # Increment the # of dirs $dirSize += $stat->size; # Increment the size of dirs return File::DirWalk::SUCCESS; # return TRUE so walk continues } ##### # p r i n t S t a t s # Print out all counters at the end of the walk ##### sub printStats { printf "%-12.12s %-15d %-20d\n", "Files", $fileN, $fileSize; printf " %-8.8s %-15d %-20d\n", "Backup", $backupN, $backupSize; printf " %-8.8s %-15d %-20d\n", "Text", $txtN, $txtSize; printf " %-8.8s %-15d %-20d\n", "PERL", $plN, $plSize; print "\n"; printf "%-12.12s %-15d %-20d\n", "Directories", $dirN, $dirSize; } ##### M a i n P r o g r a m ##### my $currdir = "."; $currdir = $ARGV[0] unless scalar(@ARGV) < 1; $currdir = dir(split("/[\\\/]", $currdir)); die "$currdir is NOT a directory" if not $currdir->is_dir(); my $walker = File::DirWalk->new; $walker->onFile(\&doFile); $walker->onDirEnter(\&doDir); $walker->walk($currdir); printStats();