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!
)