Batch Script Loops

A loop statement allows for the execution of a statement or group of statements multiple times. Batch Script has one type of loop called a 'for' loop, which can be used in a number of different ways. Unlike a lot of other programming and scripting languages, Batch Script doesn't have the concept of a 'while' loop, however, similar behaviour can be produced by using an ‘if’ statement, discussed in the previous section, along with a feature called 'labels'.

'for' Loop

A 'for' loop in Batch Script comes in a number of different forms. At its most basic, it can be used to move through a range of values and takes the following form.

for /L %%loop-counter in (start-value, step, end-value) do (
    :: Statement(s) to execute.
)

The 'start-value' is the first value in the range that needs to be used, whilst the 'end-value' is the end value for the range. The 'loop-counter' is the current value in the range and 'step' refers to how the 'loop-counter' gets incremented, for example, a 'step' of one would mean that the 'loop-counter' gets incremented by one each time through the loop. The /L signifies that a range, or list, of values is to be used.

The following example displays the value of the loop counter, 'i', for each iteration of the loop.

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

Here, start and end values are set to one and five respectively, with a step value of one, meaning that the loop counter will increment by one each time through the loop. The values of the loop counter, one through five, are displayed out to the console.

1
2
3
4
5

Another example use of a 'for' loop is its ability to interrogate the file system. The following example lists all files with a '.docx' file extension, in the console, that reside in a folder called 'Demo', on the 'C' drive. The '/R' makes the operation recursive, so the list will include files in the subfolders as well.

for /R C:\Demo\ %%f in (*.docx) do (
    echo "%%f"
)

Some example output from this can be seen below.

"C:\Demo\003.docx"
"C:\Demo\006.docx"
"C:\Demo\008.docx"
"C:\Demo\Document1.docx"
"C:\Demo\Document2.docx"
"C:\Demo\Document4.docx"
"C:\Demo\Document5.docx"
"C:\Demo\Document7.docx"
"C:\Demo\Document9.docx"
"C:\Demo\Subfolder\009.docx"

'while' Loop Equivalent

As previously mentioned, Batch Script does not explicitly have a 'while' loop, like a lot of other languages, however, there is a way to implement something similar using an ‘if’ statement, along with a feature of Batch Script called 'labels'. A 'while' loop in other languages has a condition to get in to the loop, so potentially the statements within may never execute if the condition is not met. Below is the alternative to a 'while' loop in Batch Script.

:label
if expression (
    :: Statement(s) to execute.
    goto :label
)

Here, the 'label' can be any word, except for a reserved word, but must be the same as that shown after the 'goto' statement. If the condition in the ‘if’ statement is met, then the statements within get executed. The last statement, 'goto', returns the program to where the label is defined at the top and execution of the ‘if’ statement starts again.

The below example sets a variable 'i' to one. A label is then declared called 'while', which is followed by an ‘if’ statement, that checks if the variable 'i' is less than or equal to five. If it is, the value of 'i' is output to the console and then incremented by one. The 'goto' statement takes the program back to where the 'while' label is defined, before the ‘if’ statement and the code is repeated. This happens until the variable 'i' contains a value greater than five.

set /A i = 1

:while
if %i% leq 5 (
    echo %i%
    set /A i += 1
    goto :while
)

The output will be the same as in the first ‘for’ loop example above. Note that if the variable ‘i’ had been declared and initialised to six or above, then the statements within the ‘if’ statement would not be executed at all.