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 execute the code several times, with each time with different values.

DOS supports the following syntax for loop

FOR element in the list
 DO commandtoexecute

element is a character to picks an item from a list

Do command tells to execute the command for every iteration.

how to 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 the following

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