EnableDelayedExpansion
EnableDelayedExpansion is a feature of the Windows command interpreter (cmd.exe) that controls how environment variable values are expanded in batch scripts. When enabled, variables inside a code block can be expanded at execution time using !VAR! instead of being expanded at parse time with %VAR%. This allows a script to reflect changes to variables made inside loops or other blocks.
EnableDelayedExpansion is activated with the setlocal command: setlocal EnableDelayedExpansion. The effect lasts until endlocal or the
In practice, use !VAR! inside blocks to see updated values. Example:
setlocal EnableDelayedExpansion
)
- Percent expansions (%VAR%) are still evaluated when the block is parsed, so they may not reflect
- Literal exclamation marks can be problematic inside a block with delayed expansion. Printing a literal ! often
EnableDelayedExpansion is commonly used to compute counters, accumulate values, or otherwise react to changes within loops