FOR Command

FOR is by no doubt the most useful tool that batch programming provides. It enables processing of numerous type of objects, files, command output, integers. Thus FOR is a really interresting tool to create advanced scripts.

FOR loop may seem difficult to use at first sight, nonetheless it is the most important tool to batch programming.

Synopsis - First syntax

The first syntax allows processing strings.

FOR %%A IN (string) DO (
 :: some commands 
)

Splits a string in chunks and execute the specified commands on each of them.

Example :

FOR %%A IN (some stuff "some stuff") DO (
 ECHO current element : %%A
)

Output :

current element : some
current element : stuff
current element : "some stuff"

Synopsis - Second syntax

The second syntax is based on the /f switch. This allow making some transformations on string, commands or files. Note that strings are not split the same way as using the preceding syntax.

FOR /F [options] %%A IN (object) DO (
 :: some commands
)

Execute some command on a string, a command output or a file. To ease the reading of this page, parameters and switches described in the preceding paragraph will not be described again.

Example 1: String parsing

:: The first example parses simple strings
:: from http headers

FOR /F "tokens=1,* delims=: " %%A IN ("Content-Type: text/plain
Connection: keep-alive") DO (

 :: 'delims=: ' select either of ':' and ' ' as delimiters
 :: 'tokens=1,*' assigns :
 ::   - The first token to %%A
 ::  - All the remaining tokens to %%B
 :: A ' ' following a ':', is considered as a single
 :: delimiter
 ECHO The header field is : %%A
 ECHO Its value is : %%B
)

Output:

The header field is : Content-Type
Its value is :  text/plain
The header field is : Connection
Its value is : keep alive

Example 2: Listing sub directories

FOR /F "tokens=*" %%A IN ('dir /b /a:D') DO (

 :: "tokens=*" assigns the full line to %%A

 :: The loop uses ``dir /b /a:D'' output to
 :: print only the sub-dirs

 ECHO Sub-dir : "%%A"

)

If this loop is run on the ``bin’’ folder of pBat, the following output is obtained.

Sub-dir : "share"
Sub-dir : "cmd"

Example 3: Parsing a .csv file

Suppose we have a file named infos.csv containing.

Wally,British,Waldo's counterpart in Britain
Waldo,American,Charlie's counterpart in US
Charlie,French,Wally's counterpart in France

It consist of names of Wally from the books Where is wally in different countries, followed by their nationality and a brief description.

Donc il s’agit d’une liste d’hommmes célèbres qui on contribué à la théorie informatique; Nous pouvons l’afficher avec une boucle FOR:

(Note the variant on the option syntax.)

FOR /F "tokens=1,2,3" "delims=," %%A IN (infos.csv) DO (
 :: Separates different fields
 :: %%A receives name
 :: %%B receives nationality
 :: %%C receives brief

 ECHO %%A is %%C. He is %%C
)

The script output is:

Wally is British. He is Waldo's counterpart in Britain
Waldo is American. He is Charlie's counterpart in US
Charlie is French. He is Wally's counterpart in France

Synopsis - Third syntax

The third syntaxe allows doing traditional FOR loops (ie. a loop incrementing a variable every new loop; in a somehow C-like manner).

FOR /L %%A IN (start,step,end) DO (
 :: Some commands
)

The FOR loop requires start, step and end to be integers. If floating point numbers are specified, they will be floored. If string is specified, the behaviours is not defined.

Example: Counting from 1 to 10:

FOR /L %%A IN (1,1,10) DO (
 ECHO %%A
)

Output:

1
2
3
4
5
6
7
8
9
10

Synopsis - Deprecated syntaxes

The following syntaxes have been deprecated since FOR no allows to achieve more robust, more flexible alternatives to theses.

The first deprecated syntax enables obtaining files within a folder and its sub-folders that match a given regular expression.

FOR /R basedir %%A IN (regexp) DO (
 :: some stuff
)

This syntax is deprecated since the same result can be achieved using:

FOR /F "tokens=*" %%A IN ('dir /b /s /a:-D basedir/regexp') DO (
 :: some stuff
)

The second syntax behaves pretty much the same way as the preceding except that it requires the /D switch and selects only folders matching regexp.

Compatibility

Mostly compatible with cmd.exe. However, any code from cmd.exe can be executed with no bugs under pBat concerning FOR loops.

However, one can come across some unexpected behaviour using the tokens options. Indeed, if tokens specifiers are not specified from the lowest to the highest, then cmd.exe will silently try to sort the tokens. However pBat does not do so, leading to a potential unexpected behaviour. In addition, pBat performs all right when two tokens specifiers overlap (eg. “4-6,5-7”) although cmd.exe fails to handle such specifiers correctly.

Also, pBat does process empty lines while using /F switches while cmd.exe keeps skipping them. However this behaviour may be changed using CMDLYCORRECT.

The FOR command is availables since version 0.7.0.0. The FOR /F command is totally functionnal since version 0.7.0.2.

See also

FOR Command, IF command