Delayed Expansion will cause variables to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL command. When delayed expansion is in effect variables may be referenced using !variable_name! (in addition to the normal %variable_name% )
Set _var=second& Echo %_var% !_var!
You might expect that to produce the output second second but in fact it will output: first second
Looking at the line in bold above,
the line is parsed and the variable %_var% is expanded to "first"
Then the line is executed (changing the value of _var)
finally !_var! is expanded (because it is using delayed expansion) and it now shows the updated value "second"
In the same way, setting DelayedExpansion also affects the point at which escape characters (^) and redirection characters are evaluated:
@echo off Setlocal Set _html=Hello^>World Echo %_html%
In the above, the Echo command will create a text file called 'world' - not quite what we wanted! This is because the variable is expanded at parse time, so the last line is executing Echo Hello > World and the > character is interpreted as a redirection operator.
If we now try the same thing with EnableDelayedExpansion:
With delayed expansion, the variable (including the > ) is only expanded at execution time so the > character is never interpreted as a redirection operator.
This makes it possible to work with HTML and XML formatted strings in a variable.
Delayed variable expansion is often useful when working with FOR Loops, normally an entire FOR loop is evaluated as a single command even if it spans multiple lines of a batch script.
This is the default behaviour of a FOR loop:
@echo off setlocal :: count to 5 storing the results in a variable set _tst=0 FOR /l %%G in (1,1,5) Do (echo [%_tst%] & set /a _tst+=1) echo Total = %_tst% C:\> demo_batch.cmd      Total = 5
Notice that when the FOR loop finishes we get the correct total, so the variable correctly increments, but during each iteration of the loop
the variable is stuck at it's initial value of 0
The same script with EnableDelayedExpansion, gives the same final result but also displays the intermediate values:
@echo off setlocal EnableDelayedExpansion :: count to 5 storing the results in a variable set _tst=0 FOR /l %%G in (1,1,5) Do (echo [!_tst!] & set /a _tst+=1) echo Total = %_tst% C:\> demo_batch.cmd      Total = 5
Notice that within the for loop we use !variable! instead of %variable%.
A one line example - Setting and then Echoing the same variable within a FOR command:
for /f %%G in ("abc") do ( set _demo=%%G & echo !_demo!)
Example of replacing one variable with values from another:
@echo off setlocal EnableDelayedExpansion Set var1=Hello ABC how are you Set var2=ABC Set result=!var1:%var2%=Beautiful! Echo [!result!]
One downside of DelayedExpansion is that processing a file with a ! in the filename will be interpreted as a variable, this is not a common occurence, but it can cause scripts to fail.
An alternative method for achieving the above is CALL SET
EnableDelayedExpansion is Disabled by default.
EnableDelayedExpansion may also be enabled by starting CMD with the /v switch.
EnableDelayedExpansion can also be set in the registry under HKLM or HKCU:
1=enabled 0=disabled (default)
“At times it is folly to hasten at other times, to delay. The wise do everything in its proper time” - Ovid
Forum discussion - EnableDelayedExpansion
OldNewThing - Longer explanation of EnableDelayedExpansion
SETLOCAL - Start localisation of environment changes in a batch file.