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 $_ : $!"; }

In reply to Re: Recursive Directory Tree Deletion in Windows by the_slycer
in thread Recursive Directory Tree Deletion in Windows by gaggio

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.