QUESTION :
I’d like to know if it’s possible to process a variable containing a variable not immediately but only when asked.
For example this batch:
@echo off
setlocal EnableDelayedExpansion
set VA=Two
set VAR=!VA! people
set VA=Three
ECHO !VAR!
Prints “Two people”.
Is it possible to do the parsing only at the end with the echo to have “Three people” as output?
ANSWER :
That will not be possible. Once a variable is assigned a value, it is no longer dependent on the variable or text that was used when assigned.
The easiest way to achieve your desired output is to simply echo the VA variable and the text at the end. Basically, do all your computations, then at the end put together your output.
@echo off
setlocal EnableDelayedExpansion
set VA=Two
set VA=Three
ECHO !VA! people
pause
Just set VAR at the end again. There is no limitation in how often you can set a variable.
Remember, a batch file is only a list of commands you could type in the command prompt. Every row is basically one command at a time executed in chronological order.
So if you need to have VAR elevated with the right conditions, set it just before that moment. Example:
@echo off
setlocal EnableDelayedExpansion
set VA=Two
set VAR=%VA% people
echo %VAR%
set VA=Three
::We want to update VAR, so lets set it again:
set VAR=%VA% people
echo %VAR%
pause
:: in a batchfile is the same as REM