Why are my set commands resulting in nothing getting stored?
Asked Answered
D

1

23

I am trying to access the value of TOMCAT_VER later on, but it appears as an empty string.

if exist "%_REALPATH%\tomcat-%TOMCAT_VER2%" (
  set CATALINA_HOME=%_REALPATH%\tomcat-%TOMCAT_VER2%
  set TOMCAT_VER=%TOMCAT_VER2%
  echo "%TOMCAT_VER%"
) else if exist "%TOMCAT_VER2%" (
  set CATALINA_HOME="%TOMCAT_VER2%"
  set TOMCAT_VER="%TOMCAT_VER2%"
  echo "%TOMCAT_VER%"
)

To further debug, I inserted an echo statement right below where it gets set, but it doesn't seem to work. With echo off disabled, I can see the statement showing these variables getting set, and yet I can't seem to print them out.

Dorso answered 15/1, 2013 at 21:13 Comment(0)
K
39

You found the bbb (batch beginner bug), but not the variable is empty, it's the expansion that doesn't work as expected.

Percent expansion is done when a line or a complete parenthesis block is parsed, before the code will be executed.
But to solve this you can use the delayed expansion, this doesn't expand at parse time, it expands just at execution time.

EnableDelayedExpansion adds an additional syntax to expand variables: !var!.
The percent expansion %var% is still availabe and isn't changed by the delayed expansion.
The delayed expansion of !var! is done when the expression is executed, in spite of %var%, that will be expanded in the moment of parsing (complete code blocks), before any of the commands in the blocks are executed.

setlocal EnableDelayedExpansion

if exist "!_REALPATH!\tomcat-!TOMCAT_VER2!" (
  set "CATALINA_HOME=!_REALPATH!\tomcat-!TOMCAT_VER2!"
  set "TOMCAT_VER=!TOMCAT_VER2!"
  echo !TOMCAT_VER!
) else if exist "!TOMCAT_VER2!" (
  set "CATALINA_HOME=!TOMCAT_VER2!"
  set "TOMCAT_VER=!TOMCAT_VER2!"
  echo !TOMCAT_VER!
)
Kindness answered 15/1, 2013 at 21:19 Comment(5)
Hmm. It only seems to be printing, literally, "!TOMCAT_VER!". Do you happen to know what version of windows this was added?Dorso
Ahh, it appears in another blog entry, that enabledeplayedexpansion is only available after XP. For XP, it must be set in the registry. Need to see if we must support XP or not.Dorso
No it works also in XP, but you spell it wrong. ...deplayed.. instead of ...delayed...Kindness
Perhaps you could emphasize that delayed expansion requires the exclamation mark for variable expansion. Just setting EnableDelayedExclamation is not sufficient. Took me some time to find out. ;-)Gilson
@Gilson I added an explanation for !var! vs %var%Kindness

© 2022 - 2024 — McMap. All rights reserved.