The For command is a powerful shell scripting command because it’s versatile. Let’s look at some common uses of the For command. Specifically, let’s look at how to use the For command with the /d switch and with the /f switch. Along the way, you’ll learn about the Ping command.
Using the For /d Command
Suppose you want to test the connections to five file servers (i.e., fileserv1, fileserv2, fileserv3, fileserv4, and fileserv5). If you’ve installed the TCP/IP protocol in your network, you can use the Ping command to verify the connections. The Ping command’s syntax is
where computername is the name of the server you want to ping. By default, the Ping command sends up to four ping packets to the computer. Pinging five file servers up to four times can waste time and bandwidth, so let’s ping each server only once. To change the default number of ping packets, you use the –n parameter, which lets you specify the number of ping packets to send. To ping each server once, you can use the code
Ping -n 1 fileserv1 Ping -n 1 fileserv2 Ping -n 1 fileserv3 Ping -n 1 fileserv4 Ping -n 1 fileserv5
To shorten this code from five lines to one line, you can use the For command. The For command has different syntaxes for different purposes. In this case, the syntax to use is
For \[/d\] %variable in (set) Do command
You use this particular syntax to iterate through a set of files or directories and perform a command for each file or directory in that set. Let’s look at the individual segments in this syntax.
For \[/d\]. When you use the For command, the command processor assumes you want to iterate through files by default. To tell the command processor you want to iterate through directories, you include the optional /d switch. (In syntax, placing a switch or parameter in square brackets means it’s optional.) In the For command for the ping test, you need to include the /d switch.
%variable. The %variable parameter represents an iterator variable. An iterator variable serves as a temporary container for data that you've captured in a For command. In other words, an iterator variable exists only within that particular For command. Typically, the first iterator variable is named %i; subsequent iterator variables proceed through the alphabet from that point (e.g., %j, %k). When you use iterator variables, you need to remember two conventions. First, the letter must be lowercase. Second, if you’re running the For command from the command-shell window, you use only one percent sign (e.g., %i). In a script, you must use two percent signs (%%i); otherwise, the script will fail.
in (set). In this segment, the (set) parameter represents the files or directories through which you want the For command to iterate. In the For command for the ping test, the set is (fileserv1 fileserv2 fileserv3 fileserv4 fileserv5). The For command will iterate through this set, temporarily assigning each server name to the %i variable.
Do command. In this segment, the command parameter represents the command sequence that you want to execute for each file or directory in the set. In the For command for the ping test, the command sequence is
Do @Ping -n 1 %i
Instead of specifying a server name for the Ping command’s computername parameter, you specify the %i variable. Prefixing the Ping command with the at (@) sign tells the command processor to turn off the command-echoing feature for that command. (For more information about disabling the command-echoing feature, see "Shell Scripting 101, Lesson 1.")
If you put all the segments together, the For command for the ping test looks like
For /d %i in (fileserv1 fileserv2 fileserv3 fileserv4 fileserv5) Do @Ping -n 1 %i
This For /d command produces the same result as the five lines of Ping code.
Using the For /f Command
If you have many servers to ping, listing all the servers in a set can get cumbersome. A better alternative is to put the server names in an input file. An input file is a file that provides input to a command or script. Typically, input files are text files. Figure 1 shows a sample input file. The For /d command you just created won’t work with the input file that Figure 1 shows. Instead, you need to use a different syntax:
For /f \["options"\] %variable in (fileset) Do command
The /f switch lets you use the For command to parse a file’s contents. As the (fileset) parameter shows, you enclose the filename (or filenames—you can include more than one file in a set) in the parentheses. If you use serverlist.txt to provide the server names, the For command for the ping test looks like
For /f %i in (serverlist.txt) Do @Ping -n 1 %i
When you run this For /f command, the command processor opens serverlist.txt and reads in each line, which the /f switch parses. The /f switch parses each line into individual pieces, or tokens. By default, the command processor uses spaces or tabs to determine the tokens—that is, it uses spaces or tabs as the delimiter. So, for example, if you have server names that contain spaces (e.g., fileserv alpha, fileserv beta), the For /f command just given you wouldn’t work because the %i variable would contain only a partial server name (i.e., fileserv).
Fortunately, the For /f command provides several parsing options, which the "options" parameter in the For /f command syntax represents. The tokens and delims options are the most commonly used parsing options. The tokens option lets you specify the tokens you want to capture. The delims option lets you use other delimiters besides the default spaces and tabs. A fun way to learn about these options is to try the following experiments.
Experimenting with the Tokens Option
Open the command-shell window. Run the command
Echo This experiment should help you understand the For command better > C:\testdata.txt
to create a file called testdata.txt on your C drive. Next, run the following commands:
For /f %i in (C:\testdata.txt) Do Echo %i For /f "tokens=1" %i in (C:\testdata.txt) Do Echo %i For /f "tokens=2" %i in (C:\testdata.txt) Do Echo %i For /f "tokens=3" %i in (C:\testdata.txt) Do Echo %i
Figure 2 shows the results. As you can see, you can select any word in the text file by changing the number of the token. If you don’t use the tokens option, the first token is taken by default.
You can select all the tokens in a line by using an asterisk (*). Try the command
For /f "tokens=*" %i in (C:\testdata.txt) Do Echo %i
If you want to select the first, third, and fifth tokens, you can use the command
For /f "tokens=1,2,3,4,5" %i in (C:\testdata.txt) Do Echo %i %k %m
You’re not limited to keeping the tokens in the order in which they appear in the input file. You can, for example, display the tokens in reverse order with the command
For /f "tokens=1,2,3,4,5,6,7,8,9,10" %i in (C:\testdata.txt) Do Echo %r %q %p %o %n %m %l %k %j %i
You can even display the tokens in a comma-separated format with a command such as
For /f "tokens=1,2,3,4,5,6,7,8,9,10" %i in (C:\testdata.txt) Do Echo %i,%j,%k,%l,%m,%n,%o,%p,%q,%r
As I discussed in "Shell Scripting 101, Lesson 4," you can redirect the For /f command’s output to a file by adding the >> redirection symbol:
For /f "tokens=1,2,3,4,5,6,7,8,9,10" %i in (C:\testdata.txt) Do Echo %i,%j,%k,%l,%m,%n,%o,%p,%q,%r>>C:\testreport1.txt
Experimenting with the delims Option
In the command-shell window, run the command
Echo 10,100,55,23 > C:\testdata2.txt
to create the file testdata2.txt on your C drive. Now, run the commands
For /f "tokens=1,2,3,4" %i in (C:\testdata2.txt) Do Echo %i %j %k %l For /f "tokens=1,2,3,4 delims=," %i in (C:\testdata2.txt) Do Echo %i %j %k %l
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
to obtain the Help file.
- 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.