One of our primary reasons for learning about non-
GUI tools is to automate certain tasks. Often, however,
the difficulty of automating a task isn’t finding
the command-line tool that accomplishes it but
rather learning a necessary scripting language such
as PowerShell or VBScript. You might remember
a tool called For, which I covered in this column last year (“The
Power of For,” InstantDoc ID 96539). The For command essentially
lets you script without scripting: It lets you apply certain commands
to a series of files or folders. This month, I want to take
a look at a similar tool called Forfiles, which—in some ways—is
more powerful than For. (Windows Vista includes Forfiles in the
box for the first time, but it's been part of the resource kit since
Windows 98.)
Odd Syntax
Forfiles’ syntax is a bit ugly, but the tool is so useful that you’ll forgive
its command-line unsightliness. Basically, Forfiles first searches a set
of folders for files and folders that meet certain criteria, such as all
.exe files in the C:\myfiles folder (and its subdirectories) that have
been modified in the past 10 days. For each file or folder that Forfiles
finds, the tool creates variables—with names such as @file, @ext,
@path, and so on—in which it stores certain information (e.g., the
file or folder’s name, its extension, its full file specification, its size,
whether it’s a file or a folder, the date and time it was most recently
modified). You can then use specific options to instruct Forfiles what
to do with each of those files or folders.
Forfiles takes five basic options. The first, /p (for path) instructs
Forfiles where to search, as in /p C:\ or /p C:\datafiles. For example,
to instruct Forfiles to show you all the files in a folder named E: datafiles, you’d type
forfiles /p E:\datafiles
Of course, that’s a trivial use of the tool. You can use the Dir E:\datafiles
command to accomplish the same task. But it does illustrate a
bit of Forfiles’ syntax oddity.
What if you want to instruct Forfiles to display only files with
names that start with “t”? Anyone with a small amount of experience
with Windows command-line files would expect to type
forfiles /p E:\datafiles\t*
But that command wouldn’t work with Forfiles because the /p
option is solely for specifying a path. To use a wildcard to filter the
files and folders within that path, you need to add the /m (for mask)
option, along with the pattern you want Forfiles to use. (Why did the
author of Forfiles choose to complicate the familiar, simple methodology
of using a path with wildcards? It’s a mystery to me.)
The third option, /s ( for subdirectories), takes no parameters. As
with many other Windows command-line tools, this option simply
tells Forfiles to search not only inside the path but in any of that
path’s subdirectories.
The /d (for date) option adds the ability to select files based on
their date-modified value. You can instruct Forfiles to include only
files modified exactly n days ago by adding /d -n, where n is a number
between 0 and 32,767. So, to see only files from the E:\datafiles
folder modified three days ago, you’d type
forfiles /p E:\datafiles /d -3
Alternatively, you can instruct Forfiles to display only files modified
before or after a particular date. Typing /d +mm/dd/yyyy selects only
files modified after that date, and typing /d –mm/dd/yyyy selects
only files modified before that date. So, to display files last modified
on June 1, 2006, or before, you’d type
forfiles /p E:\datafiles /d -06/01/2006
Forfiles’ final option is /c (for command), which tells Forfiles what
to do with the files and folders that it finds. If you don’t specify the /c
option, Forfiles simply shows you the files that it’s collected. But the
/c option lets you do so much more. I’ll cover that next time.
More than For
The For command is powerful and, in some cases, easier to use than
Forfiles. However, Forfiles makes it easier to select files or folders
based on several criteria, including file size and modification date.
Furthermore, Forfiles makes it much easier to write commands that
manipulate just part of a file’s name. Once you get past the initial
learning curve, Forfiles can be a great addition to your toolkit.
Mark Minasi (www.minasi.com/gethelp) is a senior contributing
editor for Windows IT Pro, an MCSE, and the author of 25 books.