144

I'm trying to build an MSBuild script that maps a network drive to a drive letter in the script, but unfortunately the path to the target folder includes an embedded space. The embedded space causes the mapping to fail, and I don't know if it is possible to escape quotes around the path. I've tried double quote marks, but MSBuild doesn't like it (either that or Windows XP doesn't like it). Anyone know how to code this beast so the map works?

<Exec Command="net use x: \\ofmapoly703\c$\program files\ar\iap /user:$(UserID) $(Password)"
WorkingDirectory="c:\"
ContinueOnError="false"
/>

The embedded space of course occurs in "program files".

Cyberherbalist
  • 12,061
  • 17
  • 83
  • 121

4 Answers4

216

Use &quot; to encode the double quotes that you want net to see inside the Command attribute value :

<Exec Command="net use x: &quot;\\ofmapoly703\c$\program files\ar\iap&quot; /user:$(UserID) $(Password)" 
WorkingDirectory="c:\" 
ContinueOnError="false" 
/> 
vladr
  • 65,483
  • 18
  • 129
  • 130
  • 8
    This does not help when you have `Command="quot;$(PathWithTrailingBackslash)""` because it renders as `"Path\With\Trailing\Backslash\"` and the `\"` is the command-line escape sequence for `"`, so all following arguments get messed up. – jnm2 Feb 13 '17 at 23:09
  • @jnm2 Couldn't you just add another `"` after the second one? `Command="quot;$(PathWithTrailingBackslash)"""` – TetraDev Feb 20 '20 at 23:25
  • @TetraDev Then the backslash is still missing and you have an unclosed quote, the effect of which I'm not quite sure. – jnm2 Feb 21 '20 at 00:20
96

You can use single quotes for command ,e.g.

  <Exec Command='explorer.exe "$(DestinationDir)"' IgnoreExitCode="true" />

(From MSBuild exec task without blocking)

Community
  • 1
  • 1
Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
  • For me the use single quotes instead of " it's a better solution. Because the ". That could lead to problems when you try to do a XmlPoke, its going to space characters in a funny way: from " to &quot; – JavierD Feb 09 '15 at 15:57
2

Escape the quotation marks - instead of "foo bar baz", use %22foo bar baz%22.

The hex value of " is 22.

References

lonix
  • 14,255
  • 23
  • 85
  • 176
0

As detailed by @Michael-Freidgeim the solution to this is to single quote, however you still have issue with trailing slashes (in Paths) being treated as escape characters in certain circumstances, a good method to avoid that would be to follow any folder paths with a dot (.)