In a code like this:
set /p "PROGNAME=enter the name of the prog: "
set /p "VERSION=enter the version of the prog: "
setlocal enabledelayedexpansion
if "%PROGNAME%"=="foo" (
set "OPTIONS=(someParams)"
set "PROGPATH=C:\MyPath\%PROGNAME%%OPTIONS%_%VERSION%.exe"
) else if "%PROGNAME%"=="bar" (
set "OPTIONS=(otherParams)"
set "PROGPATH=C:\MyPath\%PROGNAME%%OPTIONS%_%VERSION%.exe"
)
set "PROGDLL=!PROGPATH:%PROGNAME%=%PROGNAME%dll!"
endlocal
I have learned that I need enabledelayedexpansion both for the PROGPATH variable, because it's set in the same loop than the OPTIONS variable it needs, and for the PROGDLL variable, because of the substitution.
However, in this particular case, I do not want the variables to be local. Like, not at all; I wanna access them even after the end of the script. So enabledelayedexpansion can't be used.
I then made something like this:
set /p "PROGNAME=enter the name of the prog: "
set /p "VERSION=enter the version of the prog: "
if "%PROGNAME%"=="foo" (
set "OPTIONS=(someParams)"
call set "PROGPATH=C:\MyPath\%PROGNAME%%OPTIONS%_%VERSION%.exe"
call set "PROGDLL=%%PROGPATH:foo=foodll%%"
) else if "%PROGNAME%"=="bar" (
set "OPTIONS=(otherParams)"
call set "PROGPATH=C:\MyPath\%PROGNAME%%%OPTIONS%%_%VERSION%.exe"
call set "PROGDLL=%%PROGPATH:bar=bardll%%"
)
And while this works perfectly, I am now quite lost: if all it needed was a call set, what's the point of enabledelayedexpansion ?? I mean, I've been using enabledelayedexpansion in my script each time I needed to set some dependent variables in a loop, should I replace all of those by call set and delete the enabledelayedexpansion ?
I would like to understand when I should use enabledelayedexpansion and when I should use call set, in general.
(and also, why the double %% with call set ?)