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
- 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).
- 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.
J. Steegar August 14, 2001