Reverend Phil has asked for the wisdom of the Perl Monks concerning the following question:
Now that I've gone and posted the code, I guess I should explain what the problem is. If I have a subdirectory within a subdirectory - and they have the same name - my code seems to think that it changed into that directory, but in truth it starts from scratch in the former directory.use strict; use Cwd; my $age = 365; my %data; my $hits = 0; my $dirs = 0; print "Start Time: ".scalar(localtime)."\n"; my $log = ">C:/perl/scripts/data/log.txt"; my $old_log = ">C:/perl/scripts/data/old_log.txt"; open(OLD, $old_log) or die("Unable to open $old_log:$!\n"); open(LOG, $log) or die("Unable to open $log:$!\n"); print OLD "Directory\tFile Name\tLast Accessed\n"; sub ScanDirectory{ my ($workdir) = shift; my ($startdir) = &cwd; chdir($workdir) or die("Unable to enter working dir $workdir:$!\n" +); unless ( opendir(DIR, ".") ){ chdir($startdir) or die ("Botched completely here in $startdir +<>$workdir $!\n"); print "Unable to open $workdir: $!\n"; return; } my @names = readdir(DIR) or die("Unable to read $workdir:$!\n"); closedir(DIR); ##DEBUG## print LOG "Workdir: $workdir\n"; print LOG "Startdir: $startdir\n"; print LOG "Files:\n",join("\n",@names),"\n"; ##DEBUG## foreach my $name (@names){ ##DEBUG## if($workdir =~ /Problem/){ print LOG "File: $name being checked\n"; } ##DEBUG## next if ($name eq "."); next if ($name eq ".."); $hits++; if (-d $name){ $dirs++; ##DEBUG## print LOG "IN: $workdir FROM: $startdir SCANNING: $name\n"; ##DEBUG## &ScanDirectory($name); next; } my $file_age = int(-M $name); if($file_age > $age){ my $dir = "$startdir/$workdir"; $dir =~ tr/\//\\/; print OLD "$dir\t$name\t$file_age\n"; } chdir($startdir) or die("Unable to change to dir $startdir:$!\ +n"); } } chdir("//server/folder"); &ScanDirectory("."); print "End Time: ", scalar(localtime), "\n"; print "$hits files checked, $dirs of which were Directories\n"; close(OLD);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: strange loopiness with chdir on win2k
by chromatic (Archbishop) on Feb 25, 2002 at 20:29 UTC | |
|
(Ovid) Re: strange loopiness with chdir on win2k
by Ovid (Cardinal) on Feb 25, 2002 at 20:58 UTC |