Windows 7 / Getting Started

Numerical for Loop

The variation for /l %variable in (start#,step#,stop#) do command makes the variable take on numeric values from start# to stop#, incrementing by step# at each turn. For example, the statement

for /l %v in (1,1,10) do echo %1

prints numbers from 1 to 10.The step value can be positive or negative.The set (1,1,5) generates the sequence (1 2 3 4 5), whereas the set (5,-1,1) generates the sequence (5 4 3 2 1).

Parsing Text

In its most unusual variation, the for command reads strings, the contents of a file, or the output of a command, and from this text extracts a series of values to use as the set.

This is the most complex use of the for command. In the simplest version, the command extracts just the first word from each line of text it reads.The definition of "word" is text delimited by one or more spaces or tabs.The command can be written to use any of three sources of data:

for /f %variable in (filenames) do command
for /f "usebackq" %variable in (`command1`) do command2
for /f %variable in ("literal text") do command

The first version examines all the files named in the filename set, which might use wildcards.The files are read as text files, and the first token (word) from each line is used as the source of values for the variable.

The second version runs command1 as a CMD command line and then gathers its output. The first token on each output line is used as the source of values for the variable. The third version looks at the literal text surrounded by quotes.This form would only make sense if used with environment variables within the quotes.The first token is used as the value for the variable.

If you use a set of one or more filenames and must use quotes to protect spaces in the filenames (to indicate you are specifying files and not literal text), use the usebackq modifier as in this version:

for /f "usebackq" %variable in ("filename"...) do command

Now, reading the first word from each line is not very interesting. Luckily, the parsing system also lets you choose which tokens to extract, specify token delimiters, specify a line-terminating character, and pick up more than one token from each input line.You can specify any of the following items in quotes after /f:

ModifierDescription
eol=cIndicates that all text after the character c is to be ignored.
skip=nSkips the first n lines of the file before extracting tokens.
delims=xyz...Uses the specified character(s) as the token delimiters, rather than a space and tab. For example, delims=, specifies the comma as the delimiter.
tokens=x,y,m-nSelects which tokens on the input lines to return as variables. If more than one is listed, the for command assigns the values to additional variables in increasing alphabetical order after the one specified with %. Numbers separated by a dash indicate a range. For example, tokens=1,4-7 would select input tokens 1, 4, 6, and 7 and would define four variables.
usebackqIndicates that quotation marks in set indicate filenames, not literal text, and that single quotes indicate literal text.

The following for command runs the arp command to get a list of the computer's network adapter information.The arp command lists each of the computer's IP addresses and physical MAC addresses.The for command skips three headers lines and extracts two tokens from each remaining output line.The first is stored in the named variable %a, and the second in the next letter up, %b:

for /f "skip=3 tokens=1,2 usebackq" %a in (`arp -a`) do (
    echo IP address %a, MAC address %b
)

Note One thing that for /f cannot do is parse the common comma-separated value (CSV) text format because these files use quotation marks to surround text items and can have commas inside the items. The for statement is not smart enough to parse comma- and quote-delimited text.

Also, the command I just showed works when typed directly at the command prompt, but you must double-up the percent signs when you put these commands inside a batch file.

[Previous] [Contents] [Next]