@dbenham is right on. I'll throw out an alternative. I find the delayed expansion a bit ugly and confusing for longer subroutines, so I'll tend to have a lone call
as the "body" of a for
loop. Inside the call
variable expansion happens "normally". Observe:
@echo off
setlocal
set start=1
set end=500
set interval=100
for /L %%g in (%start%, %interval%, %end%) do call :_d %%g
endlocal
goto :EOF
:_d
set /a last=%1 - 1 + interval
echo %1, %last%
For this particular case it's more code, to be sure, but for less trivial batch files I think it's a "win".
As an aside: You have no setlocal
/ endlocal
in there, so the values for first
and last
will persist across executions, assuming you keep running them in the same shell, and give you different results the second time you run your code. I tend to wrap my batch files in a setlocal
/ endlocal
pair to prevent variables I use in the script from "leaking" out into the parent shell's environment.
Finally, I'll echo what @VasiliSyrakis says: If you can avoid using cmd.exe
then, by all means, avoid it. I tend to write a lot of stuff in batch but, admittedly, it's a bit like hammering nails with a screwdriver for a lot of problems.