Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

RE: Recursive Directory Tree Deletion in Windows

by gaggio (Friar)
on Oct 18, 2000 at 20:30 UTC ( #37350=note: print w/ replies, xml ) Need Help??


in reply to Recursive Directory Tree Deletion in Windows

I am so mean, that I even give the script away now.
I think that you really should keep voting me down! (there are always going to be things that I won't understand in the world, even in nice places like PM:-( )

Note: the script might have to be run several times, because it does not delete directories when they still have files in them. - Just keep running it until 0 file and 0 directory are deleted.

#!/usr/local/bin/perl -w use File::Find; $workingdir = 'C:/TEMP/'; $countdirs = 0; $countfiles = 0; $testmode = 0; @extensions = ('txt','obj'); #files to remove @directories = ('win32-VC60','CVS','Cvs'); #directories to remove (rec +ursively with all the files/subs in them) @anymatches = ('ChangeLog'); #other special matches to remove as well find(\&processmatch, $workingdir); print "\n$countfiles file(s) and $countdirs director(y)(ies) were dele +ted.\n\n"; sub processmatch{ # Note: we are chdir in the current directory! $saved = $_; $name = $File::Find::name; $deleteflag = 0; foreach $ext(@extensions) { if($name =~ /.*\.$ext/) { $deleteflag = 1; } } foreach $dir(@directories) { if($name =~ /.*\/$dir.*/) { $deleteflag = 1; } } foreach $any(@anymatches) { if($name =~ /.*$any.*/) { $deleteflag = 1; } } if($deleteflag == 1) { if (-d $name) { if(!$testmode) { rmdir $name; } print "$name\n"; $countdirs++; } else { if(!$testmode) { unlink $saved; } print "$saved\n"; $countfiles++; } } $_ = $saved; }

BTW: Thanks for the persons who helped me!


Comment on RE: Recursive Directory Tree Deletion in Windows
Download Code
RE: RE: Recursive Directory Tree Deletion in Windows
by takshaka (Friar) on Oct 19, 2000 at 03:41 UTC
    the script might have to be run several times, because it does not delete directories when they still have files in them. - Just keep running it until 0 file and 0 directory are deleted.

    Use finddepth() instead of find() to have File::Find report all of a directory's entries before reporting the directory name itself. Depth-first searching allows you do clean out a directory before you try to delete it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://37350]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-07-22 22:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (130 votes), past polls