Batch script For Loop dynamic repeats

Jonas Huber
Jonas Huber used Ask the Experts™

i'm looking for a solution to run a For Loop a certain amount of times, which i set before in a variable.

i did something like this:
set /p count=Count:

for %%i IN (0,1,%%count%%) do

Open in new window

But no matter what i set the variable to, the code runs exact 3 times.
Where is my mistake ?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
That's because of the double percent signs around the variable, and because you need the /L option for a count loop.
Without the /L, the loop will run once for each of the elements passed inside the brackets, hence the three iterations.
set /p count=Count:
for /L %%i in (0, 1, %count%) do (
	echo %%i

Open in new window


Ok thanks that worked.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial