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


August 01, 2001

Shell Scripting 101, Lesson 8

RSS
Subscribe to Windows IT Pro | See More Batch Files Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Figure 3 shows the results of these two commands. If you don’t include the delims option, the command processor uses the default delimiter (i.e., a space, in this case), thereby reading the entire line as one token, which is why the commas appear in the first command’s result. By adding delims=, to the command, the command processor uses a comma as the delimiter, thereby reading each number as a token, which is why the second command’s result shows no commas.

In "Shell Scripting 101, Lesson 5," I showed you how to use the Set /a command to perform simple math calculations. You can use the Set /a command in a For /f command to return, then add numbers from an input file. For example, if you run the command

For /f "tokens=1,2,3,4 delims=," %i in (C:\testdata2.txt) Do Set /A Fred=%i+%j+%k+%l 

you get the result 188 (10+100+55+23).

Explore More Uses
As you can see, the For command is versatile—and I’ve only covered the most common uses of the For command. You can further explore the parameters, switches, and options the command offers by running the command

For /?|More 

to obtain the Help file.

Homework Assignments

  1. In a script, use the For /d command to open five instances of Microsoft Internet Explorer (IE) that point to your favorite Web sites. Here’s a hint on the code to launch IE: Use the path to your iexplore.exe program enclosed in quotes, followed by the URL to a favorite Web site (e.g., "C:\program files\plus!\microsoft internet\iexplore.exe" http://www.winscriptingsolutions.com).
  2. Put your five favorite URLs in an input file. Modify the script so that it uses the For /f command to parse the input file for the paths used to launch the browser instances.

End of Article

   Previous  1  2  3  4  [5]  Next  


Reader Comments
Great tutorial... fast, quick and dirty. Has enough detail and code to speed learning, but not enough to make the articles too heavy. I have done all 8 and am very impressed.

J. Steegar August 14, 2001


The tutorial was great..
But I had a quick question. How do you implement a for loop with multiple statements inside it.

Udit Budhia June 11, 2004


Superb.. Has really helped but I wanted to know if it is possible if I have a file like below...
xxxxxx,x
xxxxxx,x
xxxxxx,x
Is it possible to test for the value of the ',x' in the file so that I can run an if comment on it


0dyssey October 12, 2004 (Article Rating: )


does anyone know how to set the delimiter to the sign " ?

drohbeck February 21, 2005


FYI: Sometime the input file contains commas and spaces and other natural delimiters. I needed to set the carriage return as my "delim=". What I found is that this can be done by setting any character that is NOT present in the input file as the delimiter will cause the CR to act as the delim.

Anonymous User March 03, 2005 (Article Rating: )


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
WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Command Prompt Tricks

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

Windows 7 Sets Sales Record

Microsoft CEO Steve Ballmer described Windows 7's first ten days of sales as "fantastic" while in Japan yesterday. ...


Related Articles Programmatically Change Permissions in Print Clusters

Shell Scripting 101, Lesson 10

Shell Scripting 101, Lesson 9

Shell Scripting 101, Lesson 7

Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting 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