NealWalters
NealWalters

Reputation: 18197

Command (Batch) File - use of quotes around directory names

I found the technique below in another StackOverflow issue. It gets the proper "Program Files" directory name, whether the file runs on 32 or 64-bit version of Windows.

How can I get around the problem below. If I don't put double quotes around %programfiles% I get an error on the set.

set BTDFProgFiles=%programfiles(x86)%
if %BTDFProgFiles%=="" set BTDFProgFiles=%programfiles%
echo BTDFProgFiles=%BTDFProgFiles%
%BTDFMSBuildPath% "%BTDFProgFiles%\FRB.EC.Common\%2\Deployment\FRB.EC.Common.BizTalk.Deployment.btdfproj"  etc... 

If I put double quotes there, the SET statements work, but then the parm to the build program shows with two parms:

set BTDFProgFiles="%programfiles(x86)%"
if %BTDFProgFiles%=="" set BTDFProgFiles="%programfiles%"
echo BTDFProgFiles=%BTDFProgFiles%
%BTDFMSBuildPath% "%BTDFProgFiles%\FRB.EC.Common\%2\Deployment\FRB.EC.Common.BizTalk.Deployment.btdfproj"  etc... 

Interpreted as: "C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBu ild.exe" "C:\Program Files"\FRB.EC.BookTransfer\1.1\Deployment\FRB.EC.BookTransf er.BizTalk.Deployment.btdfproj" etc...

MSBuild thinks that I'm trying to pass more than one project.; MSBUILD: Error MSB1008: Only one projet can be specified.

Upvotes: 3

Views: 4681

Answers (1)

jeb
jeb

Reputation: 82307

You should use quotes, but in a slightly other way.

set var="my quoted string" sets var to "my quoted string"
set "var=my quoted string" sets var to my quoted string
The second method can quotes string without adding the quotes.

And to echo strings in a secure way without quotes you could use the delayed expansion.

So your code should be changed to

setlocal EnableDelayedExpansion
set "BTDFProgFiles=%programfiles(x86)%"
if "%BTDFProgFiles%"=="" set "BTDFProgFiles=%programfiles%"
echo BTDFProgFiles=!BTDFProgFiles!
"%BTDFMSBuildPath%" "%BTDFProgFiles%\FRB.EC.Common\%2\Deployment\FRB.EC.Common.BizTalk.Deployment.btdfproj"

Upvotes: 6

Related Questions