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!"
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.