Name

for

Synopsis

Repeat a specified command any number of times.

Syntax

for [/d] %%variable in (set) do command [arguments]
for /r [path] %%variable in (set) do command [arguments]
for /r [path] %%variable in (set) do command [arguments]
for /l %%variable in (start,step,end) do command [arguments]

(in and do are not options, but rather simply part of the syntax; if omitted, an error will occur.)

Description

Use this command to create loops in a batch file. A for loop is a programming construct that allows you to repeat a command for a list of items (such as filenames). You specify an arbitrary variable name and a set of values to be iterated through. For each value in the set, the command is repeated.

The options used by for are the following:

command [ arguments ]

The command to execute or the program filename to run. This can be anything you’d normally type at a command prompt; arguments are the options, if any, to pass to the command.

%% variable

A one-letter variable name that is assigned, one by one, to the elements listed in set. Although variable is treated like a standard environment variable (see set, earlier in this chapter), it’s name is case sensitive (%%j is different than %%J) and can only be one letter long. Note also the use of two preceding percent signs. If the for command is issued directly from the command prompt (and not from within a batch file), use only one percent sign here.

set

The sequence of elements through which the for command cycles. Elements are ...

Get Windows XP in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.