Skip to content

For loop

rbaltrusch edited this page Feb 15, 2021 · 9 revisions

For loop

In Batch, the for-loop is one of the most powerful tools the language provides. It gives basic, as well as extended functionality, looping through lists, number ranges, file contents, directories, strings and output of commands.

The basic for

The basic for loop may be used to loop through the elements of a list in a for:each loop fashion. The example below loops through a list containing the number 1, 2, and 3, stores each extracted element under the loop variable %%n and then outputs it to the console:

for %%n in (1,2,3) do (echo %%n)

The elements of the list may be delimited using spaces, commas or semicolons.

Note that the loop variable in batchfiles needs to be defined using %%a syntax, while a for-loop written directly in the command line needs to be defined using %a syntax.

For /F

Particularly the for loop with the /f switch is very powerful and will often be needed when writing any kind of real-life Batch.

It comes in three variants (take note of the quotes used in the brackets!):

  1. for /f %%f in ( file.txt ) do ( ... ): Loops through file contents
  2. for /f %%f in ( "this is my string" ) do ( ... ): Loops through string
  3. for /f %%f in ( 'command' ) do ( ... ): Loops through outputs of command

Tokens and delims

The /f switch for-loop has extra options that specify how the for loop will handle the contents that are being passed to it, regardless of whether we are looping through a file, a string, or command output.

Although there are more options, we will focus on tokens and delims.

The for loop processes the content that it receives and breaks it up into tokens using the specified delimiters (if none are provided, it uses the default delimiters, which include a space and line break). By default, only the first token of each line of content is returned. For example, the example below loops through the string "hello;goodbye" and specifies the delimiter as a semicolon. As only the first token is extracted, the for loop variable is set to the string hello:

for /f "delims=;" %%w in ( "hello;goodbye" ) do ( echo %%w )

Similarly, we instead extract only the second token and echo out goodbye in the example below:

for /f "tokens=2 delims=;" %%w in ( "hello;goodbye" ) do ( echo %%w )

Looping through file contents

The same rules apply to looping through file contents as have been described for looping through a string, with the exception of the slightly different syntax in the for loop, as already touched on above:

for /f %%f in ( file.txt ) do ( ... )

Looping through command output

The example below lists all variables and their contents of the current scope by using the command set and loops through them, echoing out the variable name using %%a and the variable state by using %%b:

for /f "tokens=1,2 delims==" %%a in ('set') do (echo %%a is equal to %%b)

Note that we have not declared the loop variable %%b explicitly anywhere. Instead, the for-loop creates a new variable, in alphabetic order starting from the loop variable specified (in this case %%a) for each of the tokens specified. If we had specified tokens=1,2,3, we could have accessed the tokens using %%a, %%b and %%c.

For /L

The /L switch allows looping through a number range using the syntax:

for \L %%c in (START INCREMENT STOP) do ( ... )

The example below loops through the numbers from 1 to 5 and outputs them to the console:

for /l %%c in (1 1 5) do (echo %%c)

For /R

The /R switch allows recursively looping through the folders and subfolders under the specified directory. The example below lists all folders and subfolders contained in the current directory:

for /r %%d in ( . ) do (echo %%d)

Note that the example above only showcases one use of the /R switch, the same functionality in this case could have been achieved by using dir /s.

More information

More information is available directly in the command line by using help:

help for