If you have to repeat the same piece of code inside a windows batch file you have several options to work on.

First you can think to repeat the code for each time you have to call it!!!!

Then you can think of using flow control…

For example you can use some gotos to exit from the code when it is time to do something else.

The following batch file shows how to print numbers from 1 to 10

@echo off
set loopcount=1
:loop
echo %loopcount%
set /a loopcount=loopcount+1
if %loopcount%==11 goto exitloop
goto loop
:exitloop

and the result is

C:\> .\avoidfor.bat
1
2
3
4
5
6
7
8

9
10

bash

 

Last but not least, remeber that FOR is your friend

@echo off
for /l %%X in (1,1,10) do echo %%X

and the result is

C:\> .\for.bat
1
2
3
4
5
6
7
8
9
10

the first parameter in () is the starting value for the loop, the second parameter is the increment to apply and the third is the end value for the loop.

The for command can be used to loop on integer values, into a set of values, in directory and more…

In a future post I’ll describe the other functionalities of for.

Gg1