This post is about how to use for loop in DOS programming.

Loops are used to execute a block of code multiple times.

DOS loop

Loops are easy to write and to execute the code number of times, with each time with different values.

DOS support the following syntax for loop

FOR element in the list
 DO commandtoexecute

element is a character to pick an item from a list

Do command tells to execute the command for every iteration

how iterate files in a directory using for loop

Let’s see an example.

Here is a command to execute in a command line

for /r %k in (*.\k) do echo %k

To run in a batch file, we have to include

for /r %%k in (*) do echo %%k

We can use for loop with following

For /r - recursive files and subfolders FOR /D - iterates folders FOR /L - Iterate loop of numbers