21

I am trying to call MSBuild from a command line. Everything was working fine when I was using a path that had no spaces, but now I have a path that has spaces and the command is failing.

Command (works):

"C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe" 
/t:Rebuild "C:\Projects\myProject.csproj" 
/p:OutDir=c:\temp\deploy\funAndGames\Deployment\bin\ 
/p:WebProjectOutputDir=c:\temp\deploy\funAndGames\Deployment\ 
/p:Configuration=Release

I then added quotes and changed OutDir to OutPath (doesn't work):

"C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe" 
/t:Rebuild "C:\Projects\myProject.csproj" 
/p:OutPath="c:\temp\deploy\funAndGames\Deployment\bin\" 
/p:WebProjectOutputDir="c:\temp\deploy\funAndGames\Deployment\" 
/p:Configuration=Release

What I am aiming for is something like this (doesn't work):

"C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe" 
/t:Rebuild "C:\Projects\myProject.csproj" 
/p:OutPath="c:\temp\deploy\fun and games\Deployment\bin\" 
/p:WebProjectOutputDir="c:\temp\deploy\fun and games\Deployment\" 
/p:Configuration=Release

Any help on the syntax around OutDir/OutPath and WebProjectOutputDir with spaces? Is it possible? If it isn't does anyone know what the reason is (due to some Url's not having spaces type thing?)

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
ChrisHDog
  • 4,473
  • 8
  • 51
  • 77

7 Answers7

23

Just found this out an answer to this old question. To handle spaces, you should use the escape character \ on all folders. Basically

/p:OutPath="c:\temp\deploy\fun and games\Deployment\bin\"

should be

/p:OutPath="c:\\temp\\deploy\\fun and games\\Deployment\\bin\\"

and magically it works!

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
johnofcross
  • 669
  • 1
  • 6
  • 19
5

Try add " ex:

/p:OutPath=""c:\temp\deploy\fun and games\Deployment\bin\""
stema
  • 90,351
  • 20
  • 107
  • 135
CNtw
  • 51
  • 1
  • 1
4

Msbuild also seems to work with spaces in the OutDir if you switch \ to /, while using quotes:

/p:OutDir="c:/temp/deploy/fun and games/out/"
/p:WebProjectOutputDir="c:/temp/deploy/fun and games/Deployment/"
Trey Mack
  • 4,215
  • 2
  • 25
  • 31
  • Works with `/p:AspnetMergePath="C:/Program Files (x86)/Microsoft SDKs/Windows/v8.0A/bin/NETFX 4.0 Tools/"` too, I just found out. Backslashes failed. – Trey Mack Feb 20 '17 at 21:50
2

For me the working solution is:

/p:SQLCMD="\"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE\""

In other words: Putting all the string into quotes (the external quotes aren't passed as value to MSBuild).

The value inside MSBuild for this property is: "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE" (with the quotes).

Eric Lemes
  • 561
  • 3
  • 10
1
> "C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe"
> /t:Rebuild
> "C:\Projects\myProject.csproj" 
/p:OutPath="c:\temp\deploy\funAndGames\Deployment\bin\"
----------------------------------------
/p:WebProjectOutputDir="c:\temp\deploy\fun and games\Deployment\"
----------------------------------------

> /p:Configuration=Release

Try this.

Also try via VSStudio GUI. Then copy the settings & try with MS Build.

Brant Bobby
  • 14,956
  • 14
  • 78
  • 115
Ganesh R.
  • 4,337
  • 3
  • 30
  • 46
1

If you have multiple parameters in a switch you can't really 'avoid' the problem by fixing the path. What you can do is put your parameters of the switch between " some_parameters1 some_parameters2 ".

Something like:

<Exec Command="SomeCommand /useMultipleParameterSwitch=&quot;value1:blabla1 | value2:blabla2&quot;"/>

Of course a lot depends of the syntax of the switches but that works for me and my team.

Stephen Jennings
  • 12,494
  • 5
  • 47
  • 66
0

To do this when using a .proj file and your path is included in properties like $(DeployFolder) and $(NuGetExe), you can use "&quot;" like this:

<Exec Command="&quot;$(NuGetExe)&quot; pack -OutputDirectory &quot;$(DeployFolder)&quot;" />
nickspoon
  • 1,347
  • 12
  • 18