Delayed Variable Expansion
Delayed variable expansion is a feature in Windows batch scripting that allows you to access the current value of a variable at execution time rather than at parse time. This is particularly useful when dealing with variables inside blocks of code (like if
statements or for
loops).
Key Points:
- Syntax: Uses exclamation marks
!var!
instead of percent signs%var%
- Activation: Requires
SETLOCAL EnableDelayedExpansion
at the beginning of your script - When Needed: Essential when modifying and reading variables within the same block
Example Without Delayed Expansion:
@echo off
set var=original
if 1==1 (
set var=new
echo %var% :: Will still show "original"
)
Example With Delayed Expansion:
@echo off
SETLOCAL EnableDelayedExpansion
set var=original
if 1==1 (
set var=new
echo !var! :: Correctly shows "new"
)
Mathematical Operations:
When performing calculations in loops, delayed expansion is often necessary:
SETLOCAL EnableDelayedExpansion
set /a count=0
for /l %%i in (1,1,5) do (
set /a count+=1
echo Iteration !count!
)
The main difference can be summarized with the equation: \(\text{Regular Expansion} \rightarrow \%var\% = \text{Value at parse time}\) \(\text{Delayed Expansion} \rightarrow !var! = \text{Value at execution time}\)
This feature is crucial for writing robust batch scripts that need to handle dynamic variable values within code blocks.