Some batch files on Windows use an IF syntax with multiple lines, as below:
if var==rule (
some comands
) else (
else commands
)
Now, Windows Vista x64 decided to put all 32 bits files under "C:\Program Files (x86)". Unfortunately, whenever you use an environment variable (such as PATH) inside a multiple line IF without quotes, the parenthesis inside the variable's value confuses IF, aborting the batch file. For example:
if "%OS%"=="Windows_NT" (
@setlocal
call :set_home
set _ARGS=%*
) else (
set _SCALA_HOME=%SCALA_HOME%
rem The following line tests SCALA_HOME instead of _SCALA_HOME, because
rem the above change to _SCALA_HOME is not visible within this block.
if "%SCALA_HOME%"=="" goto error1
call :set_args
)
A batch file with this will fail even though the line where %SCALA_HOME% appears does not get executed. This is rather annoying. Is there a solution for this?
Change all instances of %SCALA_HOME%
to !SCALA_HOME!
, and add the following near the top of the file:
setlocal enableextensions enabledelayedexpansion
The latter turns on "delayed variable expansion", which means that variables written in the form !VAR!
are not expanded until they are used, rather than when the statement itself is parsed. (In my limited experience, and certainly in this case, this means that variable expansions are less likely to be misinterpreted as actual batch file syntax constructs.) Thanks to Patrick Cuff for pointing out this better way of doing it in a comment.
P.S.: As you are discovering, the cmd.exe
batch file language is horribly broken in many ways. If you can't use a genuine scripting language (e.g. if your task needs to be performed on other computers), I would seriously recommend knocking out a quick C/C++ "script" that does the job, and compile it to .EXE.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With