As it happens I was working on something similar to this last nite. I spent a couple of hours on it before I discovered File::Find - I really have to read more, might be able to stop duplicating effort. Anyhow, here is the code that I wrote, someone let me know what problems there are.. other than the obvious dup of effort :-).
use strict;
unless ($ARGV[0]){
print "Please enter a string to look for: ";
$ARGV[0]=<STDIN>;
chomp @ARGV;
}
my $delkey = $ARGV[0];
my $startdir="c:/blah";
my (@dirs,
@newdirs,
@dellist);
print "searching $startdir\n";
#build a list of files/dirs:
my @list = glob("$startdir*");
#run through the list, if a dir, goes to @dir array (with a / at the e
+nd)
foreach (@list){
push (@dirs => "$_/") if -d;
if (/.*$delkey*/oi){
push (@dellist => "$_/") if -d;
}
}
#call the subroutine - this does the same thing on the next set of dir
+s down
build(@dirs);
#if there are further subdirs, call the sub again
while (@newdirs){
build($newdirs[0]);
shift(@newdirs);
}
sub build{
my @dirs=@_;
while (@dirs){
my $dir = shift (@dirs);
print "searching $dir\n";
my @list = glob("$dir*");
foreach (@list){
push (@newdirs => "$_/") if -d;
if (/.*$delkey*/oi){
push (@dellist => "$_/") if -d;
}
}
}
}
foreach (reverse @dellist){
print "found: $_ to delete\n";
chdir "$_" || die "Unable to change to $_ : $!";
unlink (<*>);
chdir "$startdir" || die "Unable to change back to $startdir : $!"
+;
rmdir "$_" || die "Unable to rmdir on $_ : $!";
}