Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


January 2009

Forfiles Processes Scripts—without Scripts!

Check out this powerful alternative to the For command
RSS
Subscribe to Windows IT Pro | See More Utilities Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

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.

End of Article



Reader Comments

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Learning Path Windows IT Pro Resources
"The Power of For"

"Counting on For"

"The Final For"


Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Understanding File-Size Limits on NTFS and FAT

A general confusion about files sizes on FAT seems to stem from FAT32's file-size limit of 4GB and partition-size limit of 2TB. ...


Related Events WinConnections and Microsoft® Exchange Connections

Deep Dive into Windows Server 2008 R2 presented by John Savill

Take Control of Storage & eDiscovery Costs with Unified Content Archiving

Check out our list of Free Email Newsletters!

Windows OSs eBooks Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

SQL Server Administration for Oracle DBAs

Related Windows OSs Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement