5.1.5. Examples of FOR command

Note

For advanced help, see FOR.

5.1.5.1. Simple Loops

A very simple FOR loop syntax that can print 1 to 4.

When we run command as shown below:

1for %%i in (1 2 3 4) do (
2    echo %%i
3)

The output is:

1
2
3
4

Sample FOR loop syntax where brackets are not used after the DO.

When we run command as shown below:

1for %%i in (1 2 3 4) do echo %%i

The output is:

1
2
3
4

5.1.5.2. Special handling of %%i and %i

When you run FOR directly from command prompt, you must use single %i. When you run FOR from a batch file, you must use %%i.

Wrong FOR command syntax. We are using %i instead of %%i in the batch file.

When we run command as shown below:

1for %%i in (1 2 3 4) do (
2    echo %i
3)

The output is:

i
i
i
i

A bad batch file that uses %i instead of %%i for FOR command.

When we run command as shown below:

1for %i in (1 2 3 4) do  echo %i

The output is:

i was unexpected at this time.

5.1.5.3. Using /L

Sample FOR loop that generates Loop using /L directive.

When we run command as shown below:

1for /L %%i in (1,1,5) do (
2    echo %%i
3)

The output is:

1
2
3
4
5

Sample FOR loop that generates Loop using /L directive, but in descending direction.

When we run command as shown below:

1for /L %%i in (5,-1,1) do (
2    echo %%i
3)

The output is:

5
4
3
2
1

5.1.5.4. Listing and processing files

Sample FOR loop that list files based on wildcard.

When we run command as shown below:

1for %%i in (*loop*.bat) do (
2    echo %%i
3)

The output is:

basic-dos-loop-1234-no-brackets.bat
basic-dos-loop-1234-single-percent-2.bat
basic-dos-loop-1234.bat
basic-dos-loop-1to5.bat
basic-dos-loop-5to1.bat
basic-dos-loop-list-batch-name.bat
basic-dos-loop-list-batch.bat
error-dos-loop-1234-single-percent-1.bat

Sample FOR loop that list files based on wildcard. It also show usage of %~ni to just print file name without extension.

When we run command as shown below:

1for %%i in (*loop*.bat) do (
2    @REM File Name only, no extension
3    echo %%~ni
4)

The output is:

basic-dos-loop-1234-no-brackets
basic-dos-loop-1234-single-percent-2
basic-dos-loop-1234
basic-dos-loop-1to5
basic-dos-loop-5to1
basic-dos-loop-list-batch-name
basic-dos-loop-list-batch
error-dos-loop-1234-single-percent-1

5.1.5.5. Processing contents of a file

Assuming we have a text file like this, Listing chracters of Star Wars and their birth place.

Anakin        Skywalker     Tatooine
Luke          Skywalker     Polis Massa (Asteroid)
Padme         Naberrie      Naboo

Sample FOR loop which can process a text file.

When we run command as shown below:

1FOR /F "tokens=1,2 delims= " %%i in (star_wars_characters.tsv) do (
2    echo First Name=%%i  Last Name=%%j
3)

The output is:

First Name=Anakin  Last Name=Skywalker
First Name=Luke  Last Name=Skywalker
First Name=Padme  Last Name=Naberrie

Sample FOR loop which can process a text file. (But is not able to handle the last column because it has values with many spaces/tabs).

When we run command as shown below:

1FOR /F "tokens=1,2,3 delims= " %%i in (star_wars_characters.tsv) do (
2    echo %%i %%j was born on %%k
3)

The output is:

Anakin Skywalker was born on Tatooine
Luke Skywalker was born on Polis
Padme Naberrie was born on Naboo

Sample FOR loop which can process a text file. (But it handles multiple spaces in the last column very well)

When we run command as shown below:

1FOR /F "tokens=1,2,* delims= " %%i in (star_wars_characters.tsv) do (
2    echo %%i %%j was born on %%k
3)

The output is:

Anakin Skywalker was born on Tatooine
Luke Skywalker was born on Polis Massa (Asteroid)
Padme Naberrie was born on Naboo