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