Command: for
FOR runs a specified command for each file in a set of files.
FOR is a COMMAND LINE and a BATCH-FILE / AUTOEXEC.BAT /FDAUTO.BAT
command.
Syntax:
FOR %variable IN (set) DO command [cmd-parameters] OR:
FOR %%variable IN (set) DO command [cmd-paramters]
Options:
%variable A name for the parameter that will be replaced
with each file name. The variable may only be one
character long.
%%variable A name for the parameter that will be replaced
with each file name. The variable may only be one
character long.
(set) Specifies a set of one or more files. Wildcards
and ? may be used.
command Specifies the command to run for each file.
cmd-parameters Specifies parameters or switches for the
specified command.
Comments:
The FOR command does NOT distinguish if ONE or more percent characters
are used in COMMAND LINE or in BATCH FILES (tested). The variables are
case sensitive and maybe only ONE character long.
FOR is a command internal to command.com and needs no other file
in order to work.
Examples:
IN COMMAND LINE:
Example 1:
Displays all the text files in the current directory, one after
another:
FOR %f in (*.txt) DO more %f
Example 2:
The following two commands list all files starting with an "a". The
list starts with "---start---" and ends with "---end---". The "-"
sign at the beginning and at the end of each line comes from "DO
ECHO - %f -".
ECHO off
FOR %f IN (---start--- a*.* ---end---) DO ECHO - %f -
ECHO on
Example 3:
The following command writes a list of all files in the directory
into a .txt file. You need a ">>", otherwise the first written text
is overwritten by the following text:
ECHO off
for %g in (*.*) do echo %i >> echo.txt
ECHO on
Example 4:
The following command creates folder01 till folder10.
for %i in (01,02,03,04,05,06,07,08,09,10) DO MD folder%i
You can also use a space instead of a comma, e.g.:
for %j in (01 02 03 04 05 06 07 08 09 10) DO RD folder%j
Example 5:
The following command deletes all files ending with "tmp" or starting
with "temp" in a batch file:
for %%b in (*.tmp temp????.*) do del %%b
Please keep in mind that variables are case sensitive, means:
do not mix %b with %B.
See also:
autoexec.bat/fdauto.bat
batch files
command.com/freecom
set
shift
Copyright © 2003 Robert Platt, updated 2007 and 2022 by W. Spiegl.
This file is derived from the FreeDOS Spec Command HOWTO.
See the file H2Cpying for copying conditions.