Monday, May 07, 2007

ForFiles Command

I just discovered a very useful command in Windows: ForFiles. It's used to iterate over files from the command prompt (and much easier to use than the for command, when it comes to manipulating files), and even processes some criteria.

I just used this command to cleanup old (/d -30; older than 30 days) gzipped (/m *.gz) files in the path (/p) e:\tempfiles and all subdirectories (/s). For each file, it issued the command (/c) to echo the filename to the command window (so I could see which files were being deleted) and then delete the file.

forfiles /p e:\tempfiles /s /m *.gz /d -30 /c "cmd /c echo @file & del @file"

Check the Microsoft TechNet documentation for complete usage information and some more examples. If you don't already have the ForFiles command in your version of Windows (I know that it's included in Vista and Server 2003 SP1, but not XP SP2), you can get it from Microsoft's FTP site under the NT 4.0 "y2kfix" Resource Kit files.

[EDIT 2007.05.14: Apparently I posted the wrong MS FTP link. Thanks to Daniell for pointing this out and sending me the corrected link.]

3 comments:

Page Brooks said...

That's cool! I didn't even know the for command existed!

Dean Lisenby said...

For the record, I still think command line interface tools are the best...

Anonymous said...

yep not out of the box with xp sp2 thanks for the links to install and usage notes