Captain Ron
Distinguished Member
- Joined
- Jul 6, 2007
- Messages
- 11,006
- Reaction score
- 13,435
- Points
- 3,897
I'm trying to get my head around delayed variable expansion in DOS batch.
Here's two example scripts, each followed by their output.
I am trying to understand why the second one works precisely and the first doesn't.
What I don't get is why I have to do the !PREVEND! in the second script for that variable only and not for ALL the variables.
If anyone can explain this to me in plain English (for the batch noob I am) I would greatly appreciate it.
There are a lot of batch experts who understand the concept but I havn't found any who can put it into words I can understand!
Here's two example scripts, each followed by their output.
I am trying to understand why the second one works precisely and the first doesn't.
What I don't get is why I have to do the !PREVEND! in the second script for that variable only and not for ALL the variables.
If anyone can explain this to me in plain English (for the batch noob I am) I would greatly appreciate it.
There are a lot of batch experts who understand the concept but I havn't found any who can put it into words I can understand!
@echo off
for /l %%i in (0,10,106) do call :outputdata %%i
call :outputdata 106
goto end
:outputdata
if not %1==0 (
set /a PREVEND=%1-1
echo START=%PREVSTART%
echo END=%PREVEND%
)
set /a PREVSTART=%1
:end
START=0
END=
START=10
END=9
START=20
END=19
START=30
END=29
START=40
END=39
START=50
END=49
START=60
END=59
START=70
END=69
START=80
END=79
START=90
END=89
START=100
END=99
@echo off
setlocal enabledelayedexpansion
for /l %%i in (0,10,106) do call :outputdata %%i
call :outputdata 106
goto end
:outputdata
if not %1==0 (
set /a PREVEND=%1-1
echo START=%PREVSTART%
echo END=!PREVEND!
)
set /a PREVSTART=%1
:end
START=0
END=9
START=10
END=19
START=20
END=29
START=30
END=39
START=40
END=49
START=50
END=59
START=60
END=69
START=70
END=79
START=80
END=89
START=90
END=99
START=100
END=105
Last edited: