Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I am writing a script to run up and down a directory tree and count how many of a few types of file I find and how much space they take up. As it runs, PERL reports the following errors repeatedly:
This appears to happen on empty directories. The directory I am searching starts at C:\Users\Peter Lutz\Documents, which also contains some hidden and (perhaps) system files.readdir() attempted on invalid dirhandle at C:/Perl/site/lib/File/Dir +Walk.pm line 100. closedir() attempted on invalid dirhandle at C:/Perl/site/lib/File/Di +rWalk.pm line 117.
The code in DirWalk that the errors point to is:
Finally, MY code is: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;
I know that was long, but I figure better more info than less. Please understand!#!/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 'fi +le' my $stat = $file->stat(); # stat the file if (not defined $stat) { return; } # wierd error ... ig +nore file $fileN++; # Incr. # of files $fileSize += $stat->size; # Incr. file size my $base = $file->basename; # Get the base to c +heck 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 w +ith .pl) $plSize += $stat->size; } return File::DirWalk::SUCCESS; # return TRUE so wal +k 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 'd +ir' my $stat = $dir->stat(); # Stat the dir if (not defined $stat) { return; } # Wierd error ... ig +nore the dir $dirN++; # Increment the # of d +irs $dirSize += $stat->size; # Increment the size o +f dirs return File::DirWalk::SUCCESS; # return TRUE so wal +k 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();
Does anyone have experience with this, or insight into what is going on?
Thanks ... phlpittsny
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem with File::DirWalk and Active Perl
by kcott (Archbishop) on Mar 15, 2012 at 22:43 UTC | |
by Anonymous Monk on Mar 16, 2012 at 16:10 UTC | |
|
Re: Problem with File::DirWalk and Active Perl
by Marshall (Canon) on Mar 15, 2012 at 21:34 UTC |