Windows Tip Sheet

Clean Up Your Room, Revisited

A better way to find and delete files quickly and almost painlessly.

Last year, I wrote a "Tip Sheet" column about finding certain files and then deleting them. After some further thought and looking at the comment, I realized you deserve an even easier way. There's still work for you to do, but the commands you have to type are reduced with this example.

First, open a command prompt and change to the root of the drive or top level folder you're going to clean up. Type this command to create the beginnings of a batch file:

echo @echo off >deleteme.bat

Next, run this command (it's one long expression) or something like it to identify all the files you want to delete:

for /f "tokens=*" %x in ('dir *.bak,*.tmp,*.dmp /s /b /a-d') do @echo Del "%x" >>deleteme.bat

This command builds a batch file with a delete command for each file type it finds. The /s switch searches every subdirectory; /b provides a bare listing but when echoed as %x it will include the full file name and path. Because a file name might have spaces, I put %x in quotes. Finally, the /a-d switch tells DIR to find everything that isn't a directory. In my example, you could have directories that end in .TMP that won't delete properly.

When the command finishes, open deleteme.bat in Notepad and verify these are the files you want to delete. Edit as necessary and save.

Now you have a perfectly functioning batch file you can run anytime you want, even as a scheduled task. To reset, run the first echo command again to overwrite deleteme.bat. Of course, you may want to rename your last file so you have an audit trail of what you've deleted.

Before I finish, there are some common sense reminders. First, this won't delete any hidden files or those for which you don't have permission to delete. You need to be careful with temp files, especially on system drives. You're likely to get "access denied" messages or a notice that the file is in use and can't be deleted -- that's normal. You'll also want to make sure applications are closed that might create temp files, like Microsoft Word.

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.)

Finally, it is likely, especially with ephemeral files like temp files, that they may be deleted between the time you create the batch file and the time you actually run it.

Once you understand the concept here, you can extend it to automagically create batch files to do all sorts of things.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.