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
The Memory-Optimization Hoax

Don't believe the hype. At best, RAM optimizers have no effect. At worst, they seriously degrade performance. ...

VMware and the Future of Virtualization

What's next for virtualization and business IT? Windows IT Pro senior editor Jeff James speaks with VMware President and CEO Diane Greene on the future of virtualization technology. ...

Common .pst File Questions

Sue Mosher addresses some of the inevitable questions that Outlook users eventually ask about Personal Folders (.pst) files. ...


Windows OSs Whitepapers Replay for Exchange: Enterprise Protection and an Affordable Price

Are You Satisfied?

A Preliminary Look at Deployment Plans for Microsoft Windows Vista

Related Events Check out our list of Free Email Newsletters!

Windows OSs eBooks Understanding and Leveraging Code Signing Technologies

A Guide to Windows Disaster Recovery and Backup

A Guide to Windows Certification and Public Keys

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.


ADS BY GOOGLE SPONSORED LINKS FEATURED LINKS

Critical Challenges of ESI & Email Retention
Are you storing too much electronic information? Get expert legal advice and better understanding of what you are required to do as an IT professional.

Become a fan of Windows IT Pro on Facebook!
Join us on Facebook and be a fan of Windows IT Pro!

Sustainable Compliance: Are You Having a Resource Crisis?
Read this white paper to examine trends in compliance and security management and review approaches to reducing the cost and operational burden of compliance.

Rev Up Your IT Know-How with Our Recharged Magazine!
The improved Windows IT Pro provides trusted IT content with an enhanced new look and functionality! Get comprehensive coverage of industry topics, expert advice, and real-world solutions—PLUS access to over 10,000 articles online. Order today!

Get It All with Windows IT Pro VIP
Stock your IT toolbox with every solution ever printed in Windows IT Pro and SQL Server Magazine plus bonus Web-exclusive content on hot topics. Subscribe to receive the VIP CD and a subscription to your choice of Windows IT Pro or SQL Server Magazine!



Order Your Fundamentals CD Today!
Gain an introduction to Exchange, learn server security requirements, and understand how unified communications can play a role in your messaging strategies with this free Exchange CD.
Windows IT Pro Home Register About Us Affiliates / Licensing Media Kit Contact Us/Customer Service  
SQL Connected Home IT Library SuperSite FAQ Wininfo News
Europe Edition Office & SharePoint Pro Windows Dev Pro Windows Excavator 
 
 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