FOR %A IN (list) DO command [ parameters ]
|list||is a list of any elements, separated by either spaces, comma's or semicolons.|
|command||can be any internal or external command, batch file or even - in OS/2 and NT - a list of commands|
|parameters||contains the command line parameters for command.|
|In this example, command will be executed once for every element in list, using parameters if specified.|
|A special type of parameter (or even command) is
|MS-DOS and PC-DOS
(incl. Win95's MS-DOS 7.*)
|as specified under basic syntax, plus VFAT/FAT32 long file name handling with LFNFOR in MS-DOS 7.*|
|OS/2 Warp||as specified under basic syntax, though unlike in DOS, you may use redirection and conditional execution in the command string specified after DO|
|Windows NT 4/2000/XP||as specified under basic syntax, plus many new options|
|1.||%A vs. %%A|
|%A is for use on command lines only.
In all examples and syntax lines shown %A should be substituted with %%A when used in batch files.
|2.||%a vs. %A|
|The A in %A may be replaced by any character, either upper case or lower case, except numbers.
Note, however, that variables ar case sensitive, so be consistent:
will not work.
on the other hand, will.
|3.||Nesting FOR commands|
|In DOS (COMMAND.COM), nesting FOR commands is not possible.
However, by using a second command processor you may still be able to nest them:
Note that you cannot replace
In NT (CMD.EXE) nesting is possible.
Note that to nest FOR loops, each loop requires its own variable; i.e.
will lead to undesired results:
will work as planned, and display:
|4.||Commands in list|
|list may not only contain a list of parameters, it may even contain a list of commands that can be executed consecitively with the same parameters. a.k.a. command may also be %A:
This FOR loop will first type the temporary file and then delete it.
|The "elements" in list can be delimited (separated) by spaces, tabs, commas or semicolons.
As of MS-DOS 7, doublequoted strings are treated as a single element, whether they contain delimiters or not.
In NT, it is possible to define your own delimiters with
|6.||Forward slashes in list|
|If list starts with a forward slash, COMMAND.COM (except MS-DOS 7) will split the string in list into its first character following the forward slash and the remainder of the string.
This is demonstrated in the interactive FOR examples
|7.||Conditional manipulation of variables in command|
|The following does not work:
sets VAR to 3, not 1
The %VAR% in the comparison (IF "%VAR%"=="") is interpreted immediately, and thus empty, and then the FOR loop is started.
From the FOR loop's point of view, the command issued was:
The following construction can be used as a workaround:
If TEST2.BAT looks like this:
then VAR is set to 1
In NT a
|8.||Jump using GOTO in command|
will display different results for different command interpreters:
|Interactive examples||Shows the different ways in which different OSs handle "lists".|
|WHICH||UNIX-like WHICH utility for Windows NT (2 batch files), OS/2 (Rexx, also searches for DLL's) and a severely limited version for DOS (batch file)|
|Errorlevel||Show error level of previous command using
|RestHome||Restore all homedir shares on an NT Server|
|SCList||lists the properties of all shortcuts in an NT machine's Start Menu|
|DiskUse Version 5||lists the disk usage for every subdirectory (Windows 2000)|
|DiskUse Version 4.01||the same for NT 4|
|page last uploaded: 1 October 2014, 23:21|