why not use File::Find ? already written better module for you ,
On Wed, Nov 30, 2011 at 4:00 AM, < perl-win32-users-requ...@listserv.activestate.com> wrote: > Send Perl-Win32-Users mailing list submissions to > perl-win32-users@listserv.ActiveState.com > > To subscribe or unsubscribe via the World Wide Web, visit > > http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users<http://listserv.activestate.com/mailman/listinfo/perl-win32-users> > or, via email, send a message with subject or body 'help' to > perl-win32-users-requ...@listserv.activestate.com > > You can reach the person managing the list at > perl-win32-users-ow...@listserv.activestate.com > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Perl-Win32-Users digest..." > > > Today's Topics: > > 1. Problem with recursive routine (Barry Brevik) > 2. RE: Problem with recursive routine (Tobias Hoellrich) > 3. RE: Problem with recursive routine (Barry Brevik) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Mon, 28 Nov 2011 15:38:59 -0800 > From: "Barry Brevik" <bbre...@stellarmicro.com> > Subject: Problem with recursive routine > To: "perl Win32-users" <perl-win32-users@listserv.activestate.com> > Message-ID: > > <995C029A48947048B3280035B3B5433C010747B3@Stellar2k3-Exch.STELLARMICRO.LOCAL > > > > Content-Type: text/plain; charset="iso-8859-1" > > I'm having a problem with a recursive routine that enumerates a directory > tree and all of its files. It works well, except when it goes down 1 level > from the top directory, I get this message: Use of uninitialized value in > addition (+) at test61.pl line 61. > > I've been fighting this thing for a couple of hours, and I thought that it > was a variable scoping problem, but now I'm not so sure. > > The code: > > use strict; > use warnings; > use Cwd; > > # Target directory is the current directory. For consistency, > # convert '\' into '/' and add a trailing '/' to the directory > # path if it is missing. > (my $targetdir = cwd()) =~ s/\\/\//g; > unless ($targetdir =~ /\/$/) {$targetdir .= '/';} > > my $prefixFactor = 0; > > enumerateDirectory($targetdir, $prefixFactor); > > # --------------------------------------------------------- > # This routine enumerates the files in the target directory > # and traverses the directory tree downwards no matter how > # far it goes. The routine does this by being RECURSIVE. > # > # While processing directories, maintain a prefix factor which > # controls the indention of the file and directory display. > # > sub enumerateDirectory($$) > { > my ($targetdir, $prefixFactor) = @_; > my ($filename, $filesize) = ('', 0); > my $fileTotalSize = 0; > > if (opendir(my $currentDir, $targetdir)) > { > my $nxtfile = ''; > > # Enumerate each file in the current directory. > # > while (defined($nxtfile = readdir($currentDir))) > { > # If the current file is a directory, follow this logic. > if (-d $nxtfile) > { > # If the directory is '.' or '..' then ignore it. > if ($nxtfile eq '.' || $nxtfile eq '..') {next;} > > # If the directory name returned by readdir() is > # missing a trailing '/', add it here. > unless ($nxtfile =~ /\/$/) {$nxtfile .= '/';} > > # Display the directory name then increment the prefix factor. > print "\n", ' ' x $prefixFactor, "$nxtfile\n"; > > $prefixFactor += 2; > > # Call ourself with the directory name that we are following down. > enumerateDirectory($targetdir.$nxtfile, $prefixFactor); > > # Upon return from the recursive call, de-increment the prefix > factor. > $prefixFactor -= 2 if $prefixFactor > 0; > } > else > { > # If here, we have an ordinary file. Display it. > $fileTotalSize += (-s $nxtfile); # THIS IS LINE 61 REFERRED TO > IN THE ERROR MSG. > print ' ' x $prefixFactor, $nxtfile, ' ', (-s $nxtfile), "\n"; > } > } > > # After completely enumerating each directory, be sure to > # close the directory entity. > close $currentDir; > print "\n", ' ' x $prefixFactor, "$fileTotalSize)\n"; > } > } > > > ------------------------------ > > Message: 2 > Date: Mon, 28 Nov 2011 15:46:41 -0800 > From: Tobias Hoellrich <thoel...@adobe.com> > Subject: RE: Problem with recursive routine > To: Barry Brevik <bbre...@stellarmicro.com>, perl Win32-users > <perl-win32-users@listserv.ActiveState.com> > Message-ID: > <81cb148a36e8b241a8369338aa528ac385ce852...@nambx02.corp.adobe.com> > Content-Type: text/plain; charset="us-ascii" > > You are not changing the directory while traversing. Whenever you access > "$nxtfile", you'll need to access it as "$targetdir/$nxtfile". So, this > (among other things): > $fileTotalSize += (-s $nxtfile); # THIS IS LINE 61 REFERRED TO > IN THE ERROR MSG. > Needs to become: > $fileTotalSize += (-s "$targetdir/$nxtfile"); # THIS IS LINE 61 > REFERRED TO IN THE ERROR MSG. > > Cheers - Tobias > > -----Original Message----- > From: perl-win32-users-boun...@listserv.activestate.com [mailto: > perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Barry > Brevik > Sent: Monday, November 28, 2011 4:39 PM > To: perl Win32-users > Subject: Problem with recursive routine > > I'm having a problem with a recursive routine that enumerates a directory > tree and all of its files. It works well, except when it goes down 1 level > from the top directory, I get this message: Use of uninitialized value in > addition (+) at test61.pl line 61. > > I've been fighting this thing for a couple of hours, and I thought that it > was a variable scoping problem, but now I'm not so sure. > > The code: > > use strict; > use warnings; > use Cwd; > > # Target directory is the current directory. For consistency, # convert > '\' into '/' and add a trailing '/' to the directory # path if it is > missing. > (my $targetdir = cwd()) =~ s/\\/\//g; > unless ($targetdir =~ /\/$/) {$targetdir .= '/';} > > my $prefixFactor = 0; > > enumerateDirectory($targetdir, $prefixFactor); > > # --------------------------------------------------------- > # This routine enumerates the files in the target directory # and > traverses the directory tree downwards no matter how # far it goes. The > routine does this by being RECURSIVE. > # > # While processing directories, maintain a prefix factor which # controls > the indention of the file and directory display. > # > sub enumerateDirectory($$) > { > my ($targetdir, $prefixFactor) = @_; > my ($filename, $filesize) = ('', 0); > my $fileTotalSize = 0; > > if (opendir(my $currentDir, $targetdir)) > { > my $nxtfile = ''; > > # Enumerate each file in the current directory. > # > while (defined($nxtfile = readdir($currentDir))) > { > # If the current file is a directory, follow this logic. > if (-d $nxtfile) > { > # If the directory is '.' or '..' then ignore it. > if ($nxtfile eq '.' || $nxtfile eq '..') {next;} > > # If the directory name returned by readdir() is > # missing a trailing '/', add it here. > unless ($nxtfile =~ /\/$/) {$nxtfile .= '/';} > > # Display the directory name then increment the prefix factor. > print "\n", ' ' x $prefixFactor, "$nxtfile\n"; > > $prefixFactor += 2; > > # Call ourself with the directory name that we are following down. > enumerateDirectory($targetdir.$nxtfile, $prefixFactor); > > # Upon return from the recursive call, de-increment the prefix > factor. > $prefixFactor -= 2 if $prefixFactor > 0; > } > else > { > # If here, we have an ordinary file. Display it. > $fileTotalSize += (-s $nxtfile); # THIS IS LINE 61 REFERRED TO > IN THE ERROR MSG. > print ' ' x $prefixFactor, $nxtfile, ' ', (-s $nxtfile), "\n"; > } > } > > # After completely enumerating each directory, be sure to > # close the directory entity. > close $currentDir; > print "\n", ' ' x $prefixFactor, "$fileTotalSize)\n"; > } > } > _______________________________________________ > Perl-Win32-Users mailing list > Perl-Win32-Users@listserv.ActiveState.com > To unsubscribe: > http://listserv.ActiveState.com/mailman/mysubs<http://listserv.activestate.com/mailman/mysubs> > > > ------------------------------ > > Message: 3 > Date: Mon, 28 Nov 2011 16:46:47 -0800 > From: "Barry Brevik" <bbre...@stellarmicro.com> > Subject: RE: Problem with recursive routine > To: "Tobias Hoellrich" <thoel...@adobe.com>, "perl Win32-users" > <perl-win32-users@listserv.ActiveState.com> > Message-ID: > > <995C029A48947048B3280035B3B5433C010747D6@Stellar2k3-Exch.STELLARMICRO.LOCAL > > > > Content-Type: text/plain; charset="us-ascii" > > Thanks. That is a cool observation. > > -----Original Message----- > From: Tobias Hoellrich [mailto:thoel...@adobe.com] > Sent: Monday, November 28, 2011 3:47 PM > To: Barry Brevik; perl Win32-users > Subject: RE: Problem with recursive routine > > You are not changing the directory while traversing. Whenever you access > "$nxtfile", you'll need to access it as "$targetdir/$nxtfile". So, this > (among other things): > $fileTotalSize += (-s $nxtfile); # THIS IS LINE 61 REFERRED > TO IN THE ERROR MSG. > Needs to become: > $fileTotalSize += (-s "$targetdir/$nxtfile"); # THIS IS LINE > 61 REFERRED TO IN THE ERROR MSG. > > Cheers - Tobias > > -----Original Message----- > From: perl-win32-users-boun...@listserv.activestate.com > [mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of > Barry Brevik > Sent: Monday, November 28, 2011 4:39 PM > To: perl Win32-users > Subject: Problem with recursive routine > > I'm having a problem with a recursive routine that enumerates a > directory tree and all of its files. It works well, except when it goes > down 1 level from the top directory, I get this message: Use of > uninitialized value in addition (+) at test61.pl line 61. > > I've been fighting this thing for a couple of hours, and I thought that > it was a variable scoping problem, but now I'm not so sure. > > The code: > > use strict; > use warnings; > use Cwd; > > # Target directory is the current directory. For consistency, # convert > '\' into '/' and add a trailing '/' to the directory # path if it is > missing. > (my $targetdir = cwd()) =~ s/\\/\//g; > unless ($targetdir =~ /\/$/) {$targetdir .= '/';} > > my $prefixFactor = 0; > > enumerateDirectory($targetdir, $prefixFactor); > > # --------------------------------------------------------- > # This routine enumerates the files in the target directory # and > traverses the directory tree downwards no matter how # far it goes. The > routine does this by being RECURSIVE. > # > # While processing directories, maintain a prefix factor which # > controls the indention of the file and directory display. > # > sub enumerateDirectory($$) > { > my ($targetdir, $prefixFactor) = @_; > my ($filename, $filesize) = ('', 0); > my $fileTotalSize = 0; > > if (opendir(my $currentDir, $targetdir)) > { > my $nxtfile = ''; > > # Enumerate each file in the current directory. > # > while (defined($nxtfile = readdir($currentDir))) > { > # If the current file is a directory, follow this logic. > if (-d $nxtfile) > { > # If the directory is '.' or '..' then ignore it. > if ($nxtfile eq '.' || $nxtfile eq '..') {next;} > > # If the directory name returned by readdir() is > # missing a trailing '/', add it here. > unless ($nxtfile =~ /\/$/) {$nxtfile .= '/';} > > # Display the directory name then increment the prefix factor. > print "\n", ' ' x $prefixFactor, "$nxtfile\n"; > > $prefixFactor += 2; > > # Call ourself with the directory name that we are following > down. > enumerateDirectory($targetdir.$nxtfile, $prefixFactor); > > # Upon return from the recursive call, de-increment the prefix > factor. > $prefixFactor -= 2 if $prefixFactor > 0; > } > else > { > # If here, we have an ordinary file. Display it. > $fileTotalSize += (-s $nxtfile); # THIS IS LINE 61 REFERRED > TO IN THE ERROR MSG. > print ' ' x $prefixFactor, $nxtfile, ' ', (-s $nxtfile), "\n"; > } > } > > # After completely enumerating each directory, be sure to > # close the directory entity. > close $currentDir; > print "\n", ' ' x $prefixFactor, "$fileTotalSize)\n"; > } > } > _______________________________________________ > Perl-Win32-Users mailing list > Perl-Win32-Users@listserv.ActiveState.com > To unsubscribe: > http://listserv.ActiveState.com/mailman/mysubs<http://listserv.activestate.com/mailman/mysubs> > > > > ------------------------------ > > _______________________________________________ > Perl-Win32-Users mailing list > Perl-Win32-Users@listserv.ActiveState.com > To unsubscribe: > http://listserv.ActiveState.com/mailman/mysubs<http://listserv.activestate.com/mailman/mysubs> > > > End of Perl-Win32-Users Digest, Vol 63, Issue 12 > ************************************************ > -- Yours Sincerely Zeng Hong
_______________________________________________ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs