Invisible Hippo
Invisible Hippo

Reputation: 124

Batch: For loop parameter using variables

I want to create a for loop that uses user input variables in it's parameter. Here is what I have:

@ECHO off
DEL "./positions.txt"
SET /P start_position= Please enter the start position:
SET /P end_position= Please enter the end position:
SET /P step_size= Please enter the step size:
FOR /L %%A IN (%start_position%,%end_position%,%step_size%) DO (
    ECHO %%A >> positions.txt
)
ECHO( & echo.Done! "positions.txt" was generated. & echo.
pause

However this doesn't seem to work. Here is what I get on the output:

>> Done! "positions.txt" was generated.

It runs, but skips over the for loop. What is wrong?

Upvotes: 0

Views: 50

Answers (1)

SomethingDark
SomethingDark

Reputation: 14304

for /L loops use the order start, step, end.

FOR /L %%A IN (%start_position%,%step_size%,%end_position%) DO (
    ECHO %%A >> positions.txt
)

Upvotes: 3

Related Questions