FOR loops

Basic syntax:

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 %A, which will be substituded by each element from list consecutively.

Full syntax

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

 

Notes

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:
FOR %A IN (1 2 3) DO ECHO %a
will not work.
FOR %? IN (1 2 3) DO ECHO %?
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:
FOR %A IN (1 2 3) DO COMMAND /C FOR %B IN (A B C) DO ECHO %A%B
Note that you cannot replace COMMAND /C with CALL

In NT (CMD.EXE) nesting is possible.
Note that to nest FOR loops, each loop requires its own variable; i.e.
FOR %A IN (1 2) DO FOR %A IN (A B) DO ECHO %A
will lead to undesired results:
A
B
A
B


FOR %A IN (1 2) DO FOR %B IN (A B) DO ECHO %A%B
will work as planned, and display:
1A
1B
2A
2B
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:
DIR > tempfile.txt
FOR %A IN (TYPE DEL) DO %A tempfile.txt

This FOR loop will first type the temporary file and then delete it.
5. list delimiters
  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, wether they contain delimiters or not.

In NT, it is possible to define your own delimiters with FOR /F.
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:
FOR %%A IN (1 2 3) DO IF "%VAR%"=="" SET VAR=%%A
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:
FOR %%A IN (1 2 3) DO IF ""=="" SET VAR=%%A
The following construction can be used as a workaround:
FOR %%A IN (1 2 3) DO CALL TEST2.BAT %%A
If TEST2.BAT looks like this:
IF "%VAR%"=="" SET VAR=%1
then VAR is set to 1
In NT a CALL to a subroutine could be used instead.
8. Jump using GOTO in command
  FOR %%A IN (1 2 3) DO GOTO=%%A
:1
ECHO 1
GOTO End
:2
ECHO 2
GOTO End
:3
ECHO 3
:End

will display different results for different command interpreters:
  • CMD.EXE (NT and OS/2) will jump to label 1
  • COMMAND.COM (DOS) will jump to label 1, then 2, then 3, without executing any of the following commands, and then execute the ECHO 3 and following commands

Examples

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 FOR and redirection
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 May 2013, 12:46
Fileaze