Subscribe to Windows IT Pro
August 01, 2001 12:00 AM

Shell Scripting 101, Lesson 8

Windows IT Pro
InstantDoc ID #21984
Rating: (2)

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.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    7 years ago
    Mar 03, 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.

  • drohbeck
    7 years ago
    Feb 21, 2005

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

  • 0dyssey
    8 years ago
    Oct 12, 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

  • Udit Budhia
    8 years ago
    Jun 11, 2004

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

  • J. Steegar
    11 years ago
    Aug 14, 2001

    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.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.