A better way to find and delete files quickly and almost painlessly.
By Jeffery Hicks
Last year, I wrote a "Tip Sheet" column about finding certain files and then deleting them http://tinyurl.com/yvojna . After some further thought and looking at the comments, 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 any time 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 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.
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.
Comment: http://mcpmag.com/columns/article.asp?editorialsid=1731#post
Jeffery Hicks MCSE, MCSA, MCT is a senior network engineer with Visory Group. He's a contributing editor to ScriptingAnswers.com and the coauthor, with Don Jones, of "Advanced VBScript for Microsoft Windows Administrators" (Microsoft Press, http://tinyurl.com/g63nf ) and "PowerShell TFM" (SAPIEN Press, http://tinyurl.com/2v7dye ). Jeff is also the creator of several popular, script-related tools used for network and Exchange administration. He maintains a blog at http://jdhitsolutions.blogspot.com .
