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
?)
Well, your code should actually look like this:
Note, that the set variables are only available in the same
cmd.exe
instance which the batch script runs in.If you want to use
call
rather than delayed expansion, the code should be this:But regard, that this approach is slower, and you may get troubles with unwanted doubling of caret symbols
^
. Furthermore, the%%
could cause unintended expansion offor
meta-variables, when, for instance, the code is placed inside a section where%%P
is applicable,call set "PROGDLL=%%PROGPATH:…%%"
will lead to unexpected results, because the%%P
portion becomes expanded.