Posts Tagged ‘environment variable’

msdos script: environment variable in IF statement

Tuesday, December 9th, 2008

I just saw that the back up script I described earlier was partially not working, because the environment variables used inside the IF statement are replaced with their value as soon as the script reach the IF (same goes for a FOR statement, see for example this link describing the issue).

So when an variable is accessed and modified from within a IF statement of FOR loop, it needs to be accessed as !MY_VARIABLE! instead of %MY_VARIABLE%.
You still need to enable this behaviour in top of your script with
setlocal ENABLEDELAYEDEXPANSION