Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through for loop %x% times in batch

Tags:

batch-file

Okay, so let's say I have a variable, and let's call it x. And I have this loop:

for %%i in (%x%) do (
  REM --Code goes here--
)

Now, that loop would execute once, assuming x equaled something like 10. And if I wanted it to loop 10 times I could do this:

for %%i in (1 2 3 4 5 6 7 8 9 10) do (
  REM --Code goes here--
)

But say x equaled 105, how would I do that?

like image 582
BBMAN225 Avatar asked Mar 04 '13 23:03

BBMAN225


2 Answers

See for /? documentation for the /L option.

for /L %%A in (1,1,%x%) do (
    REM --Code goes here--
)
like image 136
David Ruhmann Avatar answered Nov 14 '22 23:11

David Ruhmann


If you are new to for statements, I recommend learning them

This aside here is a way I have made to loop x times without a for statement.

echo off
SETLOCAL EnableDelayedExpansion
set /p "x= times to loop:"
goto loop

:loop
echo %x%
set /a "x=!x!-1"
if "%x%" LEQ "0" (goto getout)
goto loop

:getout
cls
echo you escaped!
pause
like image 36
Factor Avatar answered Nov 14 '22 22:11

Factor