CALL with spaces and parenthesis in path

2019-06-06 08:50发布

The code given below is an excerpt from vcvarsall.bat file used to set the environment variables for Visual C++ Command Line. Due to the errors(mentioned in the code as REM statements), the environment variables are not set.

:x86

if exist "%~dp0bin" echo "%~dp0bin exists" 

REM The above line gives "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin exists" as output.

if not exist "%~dp0bin\vcvars32.bat" goto missing

call "%~dp0bin\vcvars32.bat"

REM The above line gives 'C:\Program' is not recognized as an internal or external command as output.
goto :SetVisualStudioVersion

Since the environment variables are not set I ran into runtime errors while building an nmake project.

I have gone through this SO question and this post but the workarounds didn't help.

Could anyone suggest a workaround for calling the call with spaces and parentheses in the path?

Edit:

I have placed a test echo statement in the beginning of the vcvars32.bat file which is being called. Had the file been run this statement should have been executed with the statement printed on the screen. Moreover I am sure that the program flow is through the block labelled :x86 because the test echo statements which are put there are run.

Edit 2 : After following the suggestions from Paul & Co, changed the normal set command to extended syntax that is with the quotes. On running the vcvars32.bat the execution stops at the below lines :

@if not "%WindowsSDK_ExecutablePath_x86%" == "" (
    @set "PATH=%WindowsSDK_ExecutablePath_x86%;%PATH%"
)

The error shown is:

\Microsoft was unexpected at this time.

2条回答
戒情不戒烟
2楼-- · 2019-06-06 09:19
:x86
if exist "%~dp0bin\nul" (
    rem this IF statement is useless since you are going to check if vcvars32.bat exist 
    echo "%~dp0bin exists" 
)

set "_MVS=%ProgramFiles(x86)%\Microsoft Visual Studio 12.0\VC\bin"
if not exist "%_MVS%\vcvars32.bat" (
    echo "%_MVS%\vcvars32.bat" is missing
    echo please fix it and retry again.
    exit /b 0
)
call "%~dp0bin\vcvars32.bat"

I added \nul in if exist "%~dp0bin\nul" to differentiate between file and folder.

查看更多
地球回转人心会变
3楼-- · 2019-06-06 09:29

After the help from @Jeb, I am able to resolve this error using the below tweak in vcvars32.bat.

I added

@set PATHTEMP=%PATH%
@set PATH=%PATHTEMP:"=%

before

@if not "%WindowsSDK_ExecutablePath_x86%" == "" (
    @set "PATH=%WindowsSDK_ExecutablePath_x86%;%PATH%"
)

Basically I just stripped the quotes from the PATH variable.

(Indebted to @jeb for his valuable suggestion).

查看更多
登录 后发表回答