Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


September 2007

The Power of For

It’s the ultimate Windows power tool
RSS
Subscribe to Windows IT Pro | See More User Management and Profiles Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

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



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 24, 2008

An often irreverent look at some of the week's other news, including a Vista Capable dismissal request, Zune price reductions, Morrow musings, Novell and Microsoft sitting in a tree ... two years later, Yahoo!, IE 6 on Windows Mobile, and so much more ...

Command Prompt Tricks

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

PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...


Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events Introduction to Identity Lifecycle Manager "2"

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 Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


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 Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing