Hopefully, this will be last time I will have to write a recursive code without using File::Find module. This code is being written for when I leave my current job, the person who comes after me can maintain it easily.
Now, the problem, the code executes but returns nothing. It is suppost to return "No files deleted if nothing is done", but I don't even get that. I thought that the problem was in the $workdir string, but it doesn't appear to be that. Hopefully, someone, can shed some light and wisdom my way.
#!/usr/bin/perl -ws
use strict;
use Cwd;
my $mode;
&op;
&scandir('c:\\mydocu~1');
sub op
{
if (defined @ARGV && $ARGV[0] =~ m/-all/)
{
$mode="all";
}else{
$mode="none";
}
}
sub scandir
{
my ($workdir) = shift || die "No working directory set: $!\n";
my ($startdir) = &cwd;
chdir($workdir) || die "Unable to change to $workdir: $!\n";
opendir(DIR, ".") || die "Unable to open $workdir: $!\n";
my @files = readdir(DIR) || die "Unable to read $workdir: $!\n";
closedir(DIR) || die "Unable to close $workdir: $!\n";
foreach my $file (@files)
{
next if ($file eq ".");
next if ($file eq "..");
if ($mode eq "all")
{
if (-d $file)
{
&scandir($file);
next;
}
if (-f $file)
{
if($file =~ m/\.\d+\w+\-\d+\wm$/i ||
$file =~ m/\.log$/i)
{
unlink($file) || warn "Unable to delete: $file: $!\n";
}else{
print "No files to delete!, All script\n";
}
}
}
elsif ($mode eq "none")
{
if(-d $file)
{
&scandir($file);
next;
}
if (-f $file)
{
if($file =~ m/\.\d+\w+\-\d+\wm$/i)
{
unlink($file) || warn "Unable to delete: $workdir\/$fi
+le: $!\n";
}else{
print "No files to delete!, None script\n";
}
}
}
chdir($startdir) || die "Unable to change to $startdir: $!\n";
}
}
&scandir(".");
Does anyone have any suggestions to what the problem might be?
curtisb -- "Becareful, what you wish for, you just might get it!"