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.
Syntax:
FOR %variable IN (set) DO command
%variable | A 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. |
set | A 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. |
command | The 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. |
Notes:
- In complex batch programs, multiple values for variable can be used to distinguish different replaceable variables.
- FOR commands can contain IF clauses but not a second FOR.
- It is not hard to construct a complex FOR statement that is virtually impossible for normal mortals to decipher (even if it does work).
- By default, FOR only recognizes 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.
Examples:
- 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.
- 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 recognized and an error message is displayed.
- 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
When:
- 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:
LFNFOR On
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:
Internal
|