The Power of For
It’s the ultimate Windows power tool
August 29, 2007
Over the past couple months, I've been discussingChml, a tool I wrote to manipulate a new Windows Vista feature called integrity levels. When I created the tool, unfortunately, I never got around to building in support for wildcards. So, I was a bit frustrated whenI recently needed to raise the integrity level of all files withnames starting with "s" to the High integrity level. Ideally,I could just type
chml s* -i:h
to get the job done. Then, I realized I didn't need to build in wildcard support, because a very old and powerful Windows utility—the For command—could provide that functionality it for me.
What For Is For
For is the ultimate Windows power tool. Essentially, For'sjob is to automatically select a set of files or folders basedon a criterion that you specify, then to execute a givencommand repeatedly—once for each file. For's syntaxlooks like
for % in () do
where filenamefilter tells For which files to select, and command tells For which command to run. In my example, I want to specify a filename filter of (s*), which specifies all files (and folders) whose names start with the letter "s." So, I would type
for %a in (s*) do chml %a -i:h
In this command, For does the wildcard processing for me by looking in the current folder, seeking out the files whose names start with "s," invoking Chml once for each of those files, then returning to the folder to search for any more matching files. Running this For statement is the exact equivalent of an administrator first figuring out which files have names starting with "s," then typing a Chml statement for that file—except, of course, that it's a lot easier to let For do the work.
The command I originally wanted to run looked like
chml -i:h.
The variablename variable accomplishes the fill in the filename part of that command. As For works its way through the sequence of files that match the filename filter, it needsa place to hold the file. That's what %a is doing in my original example—%a is what Windows refers to a replaceableparameter or variable. It's a place in memory where the Forcommand, after it finds a matching file, can insert that valueinto the command, replacing %a with the filename.
Thus, if my current directory contains three files—sit .txt, hi.exe, and salt.dat—For would firstfind the sit.txt file andplace it into the %avariable. For wouldthen progress to thecommand
chml %a -i:h
and substitute sit.txt for %a, resulting in a command of
chml sit.txt -i:h
which is the exact text of the command that For would then execute. After executing that command, For would find a match in salt.dat (remember that hi.exe wouldn't match the "s*" pattern) and again build a Chml command, this time executing
chml salt.dat -i:h
For would then find no more matches and would stop.
This most basic of For's formulations will cause For tofind file matches in the current folder. You can extend thatbehavior in two ways. First, adding the /r switch after theFor command causes For to search not just the currentfolder but also any subfolders (and sub-subfolders, and soon) in that folder. For example,
for /r %a in (s*) do chml %a -i:h
Watch for More For
For is one of those little unsung Windows heroes, and evensome long-time Windows power users might not be awareof it. I've only scratched the surface of its power, so join menext month for more For.
About the Author
You May Also Like