curtisb has asked for the wisdom of the Perl Monks concerning the following question:
Thanks in advance curtisb -- "Be careful what you wish for, it may come back and bit you in the ass!"#!/usr/bin/perl -ws use strict; use Cwd; my $mode; &op; &scandir("C:/mydocu~1"); sub op { if (defined (@ARGV) $ARGV[0]=~/-all/) { $mode="all"; }else{ $mode=""; } } sub scandir { my ($workdir) = &scandir; 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; 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!\n"; } } } elsif ($mode eq "") { if(-d $file) { &scandir; 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!\n"; } } } chdir($startdir) || die "Unable to change to $startdir: $!\n"; } } &scandir(".");
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Yet another problem with recursive code
by Fastolfe (Vicar) on Nov 10, 2000 at 02:22 UTC | |
|
Re: Yet another problem with recursive code
by btrott (Parson) on Nov 10, 2000 at 02:26 UTC | |
|
Re: Yet another problem with recursive code
by Fastolfe (Vicar) on Nov 10, 2000 at 02:27 UTC | |
by tye (Sage) on Nov 10, 2000 at 04:51 UTC | |
|
Re: Yet another problem with recursive code
by blogan (Monk) on Nov 10, 2000 at 06:53 UTC | |
|
Re: Yet another problem with recursive code
by curtisb (Monk) on Nov 10, 2000 at 02:29 UTC |