Windows Batch Script: enable delayed expansion








In compound code blocks, with setting EnableDelayedExpansion, the current value will NOT assigned to the variable as expected. see the code below:
set var=1
set switch=on

if "%switch%"=="on" (
   set var=2
   echo %var%
   REM it prints 1
)
It actually prints the original value 1. To avoid such unexpected behaviour, you need to set EnableDelayedExpansion, and using !var!:
setlocal EnableDelayedExpansion
set var=1
set switch=on

if "%switch%"=="on" (
   set var=2
   echo !var!
   REM it prints 2
)





see also




No comments:

Post a Comment