Delayed Expansion will cause variables within a batch file to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL EnableDelayedExpansion command.
Variable expansion means replacing a variable (e.g. %windir%) with its value C:\WINDOWS
For simple commands this will make no noticable difference, but with loop commands like FOR, compound or bracketed expressions delayed expansion will allow you to always see the current value of the variable.
When delayed expansion is in effect, variables can be immediately read using !variable_name! you can still read and use %variable_name% but that will continue to show the initial value (expanded at the beginning of the line).
Example:
@echo off
SETLOCAL
Set "_var=first"
Set "_var=second" & Echo %_var%
This will output: first
The value of %_var% was read into memory BEFORE the Set command which changes it.
Now repeating this with Delayed Expansion:
@echo off
SETLOCAL EnableDelayedExpansion
Set "_var=first"
Set "_var=second" & Echo %_var% !_var!
This will output: first second
T
he value of the !_var! variable is evaluated as late as possible while the %_var% variable works just as before.
There are some advantages - we can swap the value of two variables in one line:
Set "var1=%var2%" & set "var2=%var1%"
The SET command was first introduced with MS-DOS 2.0 in March 1983, at that time memory and CPU were very limited and the expansion of variables once per line was enough.
Delayed Expansion was introduced some 16 years later in 1999 by which time millions of batch files had been written using the earlier syntax. Retaining immediate expansion as the default preserved backwards compatibility with existing batch files.This is not how anyone would design a language if starting from scratch, indeed PowerShell behaves like this:
PS C:\> $demo = "First"
PS C:\> $demo = "Second" ; echo $demo
Second
Because DelayedExpansion expands variables later, that means that any escape characters (^) and redirection characters in your expressions will be evaluated before the variable expansion and this can be very useful:
@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:
Setlocal EnableDelayedExpansion
Set _html=Hello^>World
Echo !_html!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.When delayed expansion is enabled AND at least one exclamation mark in a line is present, then any carets will be interpreted as an escape and so will disappear from the output:
Setlocal EnableDelayedExpansion
Echo "Hello^World"
Echo "Hello^World!"The above will output:
"Hello^World"
"HelloWorld"
Even if you double the carets ^^, which normally would act as an escape, or add an escape just before the exclamation mark, the presence of an exclamation mark anywhere in the line will still have this effect.
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 [0] [0] [0] [0] [0] Total = 5Notice 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 will stubbornly display it's initial value of 0 even as we set new values.
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 [0] [1] [2] [3] [4] Total = 5Notice that within the for loop we use !variable! instead of %variable%.
An alternative way to write this is by calling a subroutine, because this breaks out of the loop it does not need Delayed Expansion
@echo off setlocal :: count to 5 storing the results in a variable set _tst=0 FOR /l %%G in (1,1,5) Do (call :sub %%G) echo Total = %_tst% goto :eof :sub echo [%1] & set /a _tst+=1 goto :eof C:\> demo_batch.cmd [1] [2] [3] [4] [5] Total = 5
Set and then Echo the same variable within a FOR command:
Setlocal EnableDelayedExpansion
for /f %%G in ("abc") do ( set _demo=%%G & echo !_demo!)
Replace a variable_name using values from another variable:
@echo off setlocal EnableDelayedExpansion Set var1=Hello ABC how are you Set var2=ABC Set result=!var1:%var2%=Beautiful! Echo [!result!]Another method for replacing a variable named with the content of another is CALL SET
If DelayedExpansion is used in conjunction with a FOR command looping through a set of files, if any file in the set has an exclamation mark '!' in the filename, that will be interpreted like a !variable!.
Although this is not a common character used in filenames, it can cause scripts to fail. This happens because the parameter expansion (%%P) happens just before the delayed expansion phase tries to interpret my!filen!ame.txtWhen DelayedExpansion is used inside a code block (one or several commands grouped between parentheses) whose output is Piped, the variable expansion will be skipped.
When you use a pipe, both parts of the pipe will be executed in a new cmd.exe instance and these instances are started by default with disabled delayed expansion.
EnableDelayedExpansion is Disabled by default.
EnableDelayedExpansion can also be enabled by starting CMD with the /v switch.
After being turned on, Delayed Expansion can be turned off again with SETLOCAL DisableDelayedExpansion
EnableDelayedExpansion can also be set in the registry under HKLM or HKCU:
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"DelayedExpansion"= (REG_DWORD)
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
Related:
Forum discussion - EnableDelayedExpansion (many thanks to Jeb and Aacini for clarifying quite a few points).
OldNewThing - Longer explanation of EnableDelayedExpansion.
SETLOCAL - Start localisation of environment changes in a batch file.