Hi folks. Here's my oddity from Friday, though I couldn't get to our beloved site then.
I'm attempting to recurse through directories, and snag path and file names for anything which hasn't been modified in a certain amount of time. Should be really simple, but I'm getting caught in some infinite loopiness. I'm running this on a Win2K box with the lastest Activestate, and I'm accessing a WinNT server. Here's the code, with ugly debugging hubbub within ##DEBUG## nubs.
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);
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.
Pardon me if that makes no sense. But it's odd. The ##DEBUG## gump is there so that when it starts looping, I can see where it starts and pull out my hair. Also, the line with /Problem/ is there just to add extra logging when I come across the /Problem/ directory =) Feel free to test this on your own system, changing the server names and your 'problem' folder as well. Please test this on a decent sized directory tree, and try copying a folder and pasting it inside of itself. I tried to reproduce this on the simplest structure (
folder a has folder b with additional folder b inside of it) and I had no problem. Perhaps I should post this under
Seekers of Sanity, or perhaps another monk may shed some light unto my absurdity.
-=rev=-