Or simply, try this example:
Code: Select all
set counter=0
for /l %%a in (1,1,100) do (
set /a counter+=1
set myvar=%counter%
)
echo:%myvar%
echo:%counter%
myvar = 0, counter = 100. That's because Command Prompt expands %var%s to their value BEFORE execution, at the time the code is being read/parsed/interpreted (so it effects anything within parentheses, as the whole block is read before processing). It sees this:
Code: Select all
set counter=0
for /l %%a in (1,1,100) do (
set /a counter+=1
set myvar=0
)
echo:%myvar%
echo:%counter%
Delayed expansion is just that. It expands at the moment the code is executed. Command Prompt sees this as-is:
Code: Select all
setlocal enabledelayedexpansion
set counter=0
for /l %%a in (1,1,100) do (
set /a counter+=1
set myvar=!counter!
)
echo:%myvar%
echo:%counter%
myvar is now the proper value 100.
Delayed expansion allows for some other tricks too, like dynamic variables, without the requirement for the CALL expansion trick (not gonna explain that at the moment, but it allows a form of %% delayed expansion without !! enabledelayedexpansion).
viewtopic.php?f=3&t=1486Code: Select all
for /f "delims=" %%a in (textfile.txt) do (
set /a counter+=1
set log!counter!=%%a
)
for /l %%a in (1,1,%counter%) do (
echo:!log%%a!
)
Now you've logged each line to an "array" of sorts, log#, from log1 to log(the value of counter).