Windows 10 cmd script issue.
Trying to conditionally update my PATH environment variable in a .cmd script. I want to prepend something to PATH
based on the presence of another environment variable. If the environment variable isn't set, nothing should get added to the PATH. If it is set, prepend it.
Here's my minimal reproducible example. Save the following to a script.cmd
file on disk:
echo off
REM Simulate a user with a PATH that has C:\program files (x86) within it
PATH c:\program files (x86)\my company app;%PATH%
set _PERL_PATH=D:\projects\strawberry\perl\bin
set _NASM_PATH=D:\projects\nasm
if NOT "%_PERL_PATH%"=="" (PATH %_PERL_PATH%;%PATH%)
if NOT "%_NASM_PATH%"=="" (PATH %_NASM_PATH%;%PATH%)
if NOT "%_GIT_PATH%"=="" (PATH %_GIT_PATH%;%PATH%)
Then run it by executing the script.cmd
file from the command line
D:\script.cmd
Result:
D:\>echo off
\my was unexpected at this time.
The above is all a simulated example. Even if I didn't explicitly add C:\program files (x86)\my company app
to PATH to begin with, other scripts that run before this script will do something similar (namely: Visual Studio's vcvars32.bat)
I thought it had to do with spaces and missing quotes. But after some experimentation, it has something do with the (x86)\
sequence in the initial PATH.
How can I fix?