Now that you know the basics of Forfiles (forfiles.exe)—a command-line tool that, like the For command, lets you perform repetitive tasks without having to learn how to script—it’s time to delve deeper into Forfiles. Last month’s “Forfiles Processes Scripts—without Scripts! (InstantDoc ID ID100643) introduced four of Forfiles' most basic options: The /p option tells Forfiles what folder (or path) to use in its search; the /m option specifies which filenames to look for in that path; the /s option determines whether to also search subfolders; and the /d option lets you restrict the files that Forfiles operates on according to their date-modified value. Thus, the command

forfiles /p C:\windows /m *.exe

instructs Forfiles to display all .exe files in the C:\windows folder. Adding /s instructs Forfiles to search C:\windows and all its subfolders for .exe files. (Vista has more than 19,000 of those subfolders, so think twice before trying that command!). And adding /d -100 restricts the search further to only those files modified in the past 100 days. But those four options are just the start!

Further Forfiles
The greatest Forfiles functionality lies in its /c option, which lets you control what to do with the files that you find. Every time Forfiles finds a file that meets your criteria, it stores information about that file in several built-in variables whose names all start with the at symbol (@) and contain the file's name (@file), extension (@ext), name without extension (@fname), full file specification (@path), date and time it was last modified (@fdate and @ftime), size in bytes (@fsize), and status as file or folder (@isdir). You can then use these variables to construct a command that performs a particular task on the selected files (e.g., display them, delete them, move them).

The default Forfiles /c command is /c "cmd /c echo @file," which essentially just displays the filenames, making Forfiles a somewhat supercharged version of the Dir command. You can do more, however, by substituting your own /c options. For example, to delete all the .log files in the current folder, you could type

forfiles /m *.log /c "cmd /c del @file"

That functionality isn’t terribly exciting, considering that the Del command has always accepted wildcards. But what if you wanted a Del command that deleted only log files that were larger than 1 million bytes? You could type

forfiles /m *.log /c "cmd /c

   IF @fsize GEQ 1000000 (del @file)"


That example demonstrates the IF command that makes Forfiles shine. IF, a Windows command that lets you compare strings or numbers, uses the comparison operators EQU (is equal), NEQ (is not equal), LSS (is less than), LEQ (is less than or equal to), GTR (is greater than), and GEQ (is greater than or equal to). IF gets even more powerful with the addition of its partner ELSE. Suppose you want to delete all log files greater than or equal to 1 million bytes and move the rest to a folder named C:\logarchives. You could type

forfiles /m *.log /c "cmd /c

   IF @fsize GEQ 1000000 (del @file)

   ELSE (move @file c:\logarchives)"


Notice a couple of points about the syntax: First, the command information following /c must be surrounded by double quotes; and, second, when you use IF and ELSE, I recommend surrounding both the IF clause and the ELSE clause with parentheses to keep track of what you're telling the command to do. To clarify, here's another, simpler example. Suppose you want to create a simple listing of all the log files in the current folder, with each line listing the name of the file and a notation about whether that file is larger or smaller than a megabyte. That command would look like

forfiles /m *.log /c "cmd /c

   IF @fsize GEQ 1000000 (echo @file is a million bytes or larger.)

   ELSE (echo @file is under a million bytes in size.)"


Advanced Search
The bottom line is that with Forfiles' ability to search on modification date/time and to report file size—along with a little IF/ELSE work—you now essentially have a command-line version of the Advanced Search capability that Windows Explorer has offered for the past few versions of Windows. That’s what I call a useful tool.