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


February 2009

Finishing Forfiles

Explore further functionality in the "no-scripting scripting tool"
RSS
Subscribe to Windows IT Pro | See More Utilities Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

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.

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




Top Viewed ArticlesView all articles
2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

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 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...


Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

Configuration Manager SP1 and R2 Overview

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