Windows Tip Sheet

Clean Up Your Room

Two-steps to sanity ... er ... a cleaner server.

How often did your mom tell you to pick up after yourself? It's something you probably tell your users as well. It's a sad fact, but occasionally you need to do some housecleaning. Depending on your applications, you may find all sort of backup and temp files that simply need to be cleaned out. Here's an easy, two-step process.



Tech Help—Just An
E-Mail Away

Got a Windows, Exchange or virtualization question or need troubleshooting help? Or maybe you want a better explanation than provided in the manuals? Describe your dilemma in an e-mail to the editors at; the best questions get answered in this column and garner the questioner with a nifty Redmond T-shirt.

When you send your questions, please include your full first and last name, location, certifications (if any) with your message. (If you prefer to remain anonymous, specify this in your message, but submit the requested information for verification purposes.)


First, we'll use the FOR command to create a text file

FOR /f %x in ("~*.*,*.bak,*.tmp,*.dmp") do @DIR E:\Files\%x /s /b >>deleteme.txt

In parentheses and quotes, add all the file extensions you want to find separated by commas. Change the DIR path accordingly. This command will recurse from this directory and record the full path of every file found in the deleteme.txt file. The command will append to deleteme.txt if it already exists, so delete the file before running the command again or use a different file name.

When the command finishes, review deleteme.txt to make sure there's nothing you want to keep. Edit the file as needed. To delete all the files, simply run:

FOR /f "tokens=*" %x in (deleteme.txt) do @DEL "%x"

In no time at all you've have your server cleaned up and you'll be able to go out and play.

I'll come back to these techniques in another column and exploit them further.

About the Author

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog, on Twitter at and on Google Plus (http:/

comments powered by Disqus

Reader Comments:

Thu, Apr 26, 2007 Dean Springfield, MA

This is great and gets me almost everything I need. Is there a way to have the file listed include date and time information, along with the file size. But without volume, directory, and summary information?

Tue, Dec 12, 2006 Ron Chicago

I just get a "More?" prompt. Guess I don't understand how to run this command.

Thu, Dec 7, 2006 Gantry Anonymous

Thanks, David. I had to add the " /b" parameter to to your suggested syntax for it to work, otherwise in the text file it just had a bunch of volume information repeated for every entry. So, this worked for me:
For /r |:\ %x in (~*.* *.bak *.tmp *.dmp) do @DIR "%x" /s /b >>"deleteme.txt"

Thu, Dec 7, 2006 anatoliy new york

CCleaner can do a good job too.

Thu, Dec 7, 2006 David Bayer Nashville

The syntax on the command in the article is off. Default delimiter using "for /f" is a tab or space, you need the "delims=," parameter if you want a comma-separated list. It also by default only reads the first token, so you also need the "tokens=1-4" parameter if you want more than just the ~*.* files. It would be easier to use
for /r l:\ %x in (~*.* *.bak *.tmp *.dmp) do @DIR "%x" /s >>"deleteme.txt"
Replace "l:\" with the root folder that you wish to search. I put quotes around %x and deleteme.txt because the way this is written, if there's a space in either file name then the "do" portion will choke.

Thu, Dec 7, 2006 Mike South Bend

I tried this but it always stated File Not Found with the first FOR. The file types in the first list should be seperated by a space, not a comma. Also, if putting this in a BAT or CMD file, you need %%x instead of %x

Thu, Dec 7, 2006 John Portland

how does one learn what all those characters mean

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Please type the letters/numbers you see above

Redmond Tech Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.