Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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
Replies are listed 'Best First'.
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 cooling their heels in the Monastery: (9)
As of 2015-07-31 06:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls