Recursively delete files on a windows box

Computer directory listing

Image via Wikipedia

I had a large amount of files to delete from a old backup server. I didn?t want to do it through the GUI because this machine did not have much RAM. So I was looking for the syntax to do it on the command line and I found two good resources on it I wanted to share with you.

Tech-recipes had the first quick and easy solution

From the cmd line you can run

del /s *.tmp

It will delete all files ending in tmp from the current directory and all sub directories.

Next daniweb had a post on a batch file to

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S *.svn*') DO RMDIR /S /Q %%G

If you're thinking WTF?:
?
DIR /B /AD /S *.svn* lists all files that are named ".svn"
/B makes the output "bare" with nothing but the file name
/AD only lists directories
/S Displays files in specified directory and all subdirectories if? they match the listing criteria
RMDIR /S /Q [path/name] deletes the directory [path/dir] and all of it's childrenFOR /F processes each item (in this case directories) in the set IN ('[command]') by executing the DO [command]%%G is a parameter, which in this example is a directory name"tokens=*" says that all the characters output in a line (i.e. by the dir command) are assigned to the parameter %%G
See the Microsoft Command Line Reference for more on FOR:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.