7

Below is my requirement.

@echo off
cls
Set Sleep=0
:start
echo This is a loop
xx.exe yyyyy.dll /p:InputDataSource=Table:table.xml
Set /A Sleep+=1
echo DisplayingSleepvalue
echo %Sleep%
goto start

Here I want to execute xx.exe yyyyy.dll /p:InputDataSource=Table:table.xml this line for 30 times.... Could any one please help me on this.

Thanks,
Manasa

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Manasa Manthri
  • 73
  • 1
  • 1
  • 3

3 Answers3

11

Try this way:-

@echo off
cls
Set Sleep=0
:start
if %Sleep% == 30 ( goto end )
xx.exe yyyyy.dll /p:InputDataSource=Table:table.xml
echo This is a loop
Set /A Sleep+=1
echo %Sleep%
goto start
:end
echo "am 30 now"
pause
Siva Charan
  • 17,940
  • 9
  • 60
  • 95
10

You can use a GOTO loop with an incrementing counter, but a FOR /L loop is much more efficient.

for /l %%N in (1 1 30) do someCommand

Study the documentation available by typing HELP FOR or FOR /? from the command line.

If you want to repeat the whole block of code, then enclose the block in parentheses. You can use the FOR %%N variable in place of the Sleep variable.

@echo off
cls
for /l %%N in (1 1 30) do (
  echo This is a loop
  xx.exe yyyyy.dll /p:InputDataSource=Table:table.xml
  echo DisplayingSleepvalue
  echo %%N
)

It is not needed with your example, but if you want to manipulate an environment variable within a loop, then you must use delayed expansion. Normal expansion %sleep% is only expanded once when the entire loop is parsed, whereas you need the !sleep! value to be expanded at execution time for each loop iteration. Delayed expansion must be enabled before it can be used.

@echo off
setlocal enableDelayedExpansion
cls
set sleep=0
for /l %%N in (1 1 30) do (
  echo This is a loop
  xx.exe yyyyy.dll /p:InputDataSource=Table:table.xml
  set /a sleep+=1
  echo DisplayingSleepvalue
  echo !sleep!
)
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
dbenham
  • 127,446
  • 28
  • 251
  • 390
2

You've got the loop, just add some math.

@echo off
set executecounter=0
:loop
(Insert your command here)
set /a executecounter=%executecounter%+1
if "%executecounter%"=="30" goto done
goto loop
:done
echo Complete!
pause

This will increase the counter every time the command executes, and when it has executed 30 times, the program will end.

rene
  • 41,474
  • 78
  • 114
  • 152
Navigatron
  • 416
  • 1
  • 4
  • 12