Over the past couple months, I've been discussing
Chml, 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 when
I recently needed to raise the integrity level of all files with
names 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's
job is to automatically select a set of files or folders based
on a criterion that you specify, then to execute a given
command repeatedly—once for each file. For's syntax
looks like
for %<variablename> in (<filenamefilter>) do <command>
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 <fill in the filename> -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 needs
a place to hold the file. That's what %a is doing in my original example—%a is what Windows refers to a replaceable
parameter or variable. It's a place in memory where the For
command, after it finds a matching file, can insert that value
into the command, replacing %a with the filename.
Thus, if my current directory contains three files—sit
.txt, hi.exe, and salt.dat—For would first
find the sit.txt file and
place it into the %a
variable. For would
then progress to the
command
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 to
find file matches in the current folder. You can extend that
behavior in two ways. First, adding the /r switch after the
For command causes For to search not just the current
folder but also any subfolders (and sub-subfolders, and so
on) 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 even
some long-time Windows power users might not be aware
of it. I've only scratched the surface of its power, so join me
next month for more For.
End of Article