The FOR command is used to run a specified command or series of commands for each file in a set of files. FOR is usually used in batch programs but can also be used at the command prompt.


FOR %variable IN (set) DO command

%variableA single character that acts as a replaceable marker parameter. When used in batch files, %variable must be preceded by a second % sign (to become %%variable).
To avoid confusion with the batch file parameters %0 - %9, variable can be any character except the numerals 0 - 9.
%variable is replaced with each member of set until command has processed all them all.
setA list of text strings (eg. file names), separated by spaces, to be processed by command. Wildcards for filenames are recognized (but see notes below). set must be enclosed in parentheses.
commandThe command, complete with any required parameters and/or switches, to carry out for each file. Often one of the parameters will be variable - being the file in set to be operated on.


  1. In complex batch programs, multiple values for variable can be used to distinguish different replaceable variables.

  2. FOR commands can contain IF clauses but not a second FOR.

  3. It is not hard to construct a complex FOR statement that is virtually impossible for normal mortals to decipher (even if it does work).

  4. By default, FOR only recognises short (8.3) filenames in set unless a long filename is explicitly stated (no wildcards). However, if a FOR operation produces a long filename, these are handled without difficulty.
    The LFNFOR command can be used to enable full long file name support - BUT see "Bug Warning" below.


  1. To display the contents of all the files in the current directory that have the extension ".doc" or ".txt":
    FOR %v IN (*.doc *.txt) DO TYPE %v
    Each file that has the ".doc" or ".txt" extension in the current directory is substituted for the %v variable until the contents of every such file are displayed.

  2. To use this command in a batch file, it would be written:
    FOR %%v IN (*doc *txt) DO TYPE %%v
    If the double % sign is not used, the variable is not recognised and an error message is displayed.

  3. Note, however, that
    FOR %v IN (lettershome*.doc notestotheboss*.txt) DO TYPE %v
    will find no matches unless there is a previous LFNFOR On command.

Bug Warning

  • LFNFOR is On, and
  • FOR is used to process a set of files in which the set description includes a path,
then that path is only passed to the first member of the set. Subsequent members of the set do not include the specified path!

To demonstrate the way this bug works, issue the following commands:

Echo Off
FOR %v IN (C:\*.*) DO echo %v
Echo On

The output on the screen is a list of the files in the root directory of the C: drive showing their long filenames only. Note that only the first file in the list includes the full path (i.e. "C:\MSDOS.SYS, perhaps) and all the rest are bare filenames.

When LFNFOR is OFF, the same FOR command as above produces a list of the 8.3 filenames all of which include the path (as would be expected).

File Details:


If you should have any comments or suggestions,
please contact: Bob Watson
This page last revised:
December 28, 1999