for
Runs a specified command for each file, within a set of files. Windows commands reference
On Windows
Windows
for {%% | %}<variable> in (<set>) do <command> [<commandlineoptions>](*.doc)(*.doc *.txt *.me)(jan*.doc jan*.rpt feb*.doc feb*.rpt)(ar??1991.* ap??1991.*)for /d {%%|%}<variable> in (<set>) do <command> [<commandlineoptions>]for /r [[<drive>:]<path>] {%%|%}<variable> in (<set>) do <command> [<commandlineoptions>]for /l {%%|%}<variable> in (<start#>,<step#>,<end#>) do <command> [<commandlineoptions>]for /f [<parsingkeywords>] {%%|%}<variable> in (<set>) do <command> [<commandlineoptions>]for /f [<parsingkeywords>] {%%|%}<variable> in (<literalstring>) do <command> [<commandlineoptions>]for /f [<parsingkeywords>] {%%|%}<variable> in ('<command>') do <command> [<commandlineoptions>]for /f [usebackq <parsingkeywords>] {%%|%}<variable> in (<set>) do <command> [<commandlineoptions>]
To use for in a batch file, use the following syntax
for {%%|%}<variable> in (<set>) do <command> [<commandlineoptions>]To display the contents of all the files in the current directory that have the extension .doc or .txt by using the replaceable variable %f, type
for %f in (*.doc *.txt) do type %fIdentifyTo parse a file, ignoring commented lines, type
for /f "eol=; tokens=2,3* delims=," %i in (myfile.txt) do @echo %i %j %kIdentifyTo parse the output of a command by placing set between the parentheses, type
for /f "usebackq delims==" %i in (set) do @echo %iIdentifyTo display the name of each subdirectory (not the files) within a directory, type
for /d %D in ("C:\Users\*") do @echo %~nxDIdentifyTo step through a range of numbers, type
for /l %N in (1,2,9) do @echo %NIdentifyTo perform a recursive loop of all files in a directory, including subdirectories and echo their full path, when they were last modified, and their file size, type
for /r "C:\My Dir\" %A in (*.*) do echo %~ftzAIdentify{%% | %}<variable>Required. Represents a replaceable parameter. Use a single percent sign (%) to carry out the for command at the command prompt. Use double percent signs (%%) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c.(<set>)Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command. The parentheses are required.<command>Required. Specifies the command that you want to carry out on each file, directory, or text string, or on the range of values included in set.<commandlineoptions>Specifies any command-line options that you want to use with the specified command./?Displays help at the command prompt.Several platforms
Perform a command several times.
Iterate through command line parameters
for variable; do echo $variable; doneIdentifyExecute the given commands for each of the specified items
for variable in item1 item2 ...; do echo "Loop is executed"; doneIdentifyIterate over a given range of numbers
for variable in {from..to..step}; do echo "Loop is executed"; doneIdentifyIterate over a given list of files
for variable in path/to/file1 path/to/file2 ...; do echo "Loop is executed"; doneIdentifyIterate over a given list of directories
for variable in path/to/directory1/ path/to/directory2/ ...; do echo "Loop is executed"; doneIdentifyPerform a given command in every directory
for variable in */; do (cd "$variable" || continue; echo "Loop is executed") doneIdentifyUse a C-style for loop
for ((i=0; i<10; i++)); do echo $i; doneIdentifyWindows
Conditionally execute a command several times.
Execute given commands for the specified set
for %variable in (item_a item_b item_c) do (echo Loop is executed)IdentifyIterate over a given range of numbers
for /l %variable in (from, step, to) do (echo Loop is executed)IdentifyIterate over a given list of files
for %variable in (path\to\file1.ext path\to\file2.ext ...) do (echo Loop is executed)IdentifyIterate over a given list of directories
for /d %variable in (path\to\directory1.ext path\to\directory2.ext ...) do (echo Loop is executed)IdentifyPerform a given command in every directory
for /d %variable in (*) do (if exist %variable echo Loop is executed)