How to put double quotes in VS2010 post build step


Solution 1

I was having trouble using double quotes with a pre-build event command in Visual Studio. I have seen the batch file solutions to this problem, but it seems a batch file would not solve all problems and is not elegant. I found the solution was to put a space before the closing double quote. The details are as follows.

The following command worked, but would not support spaces in the path:

subwcrev $(SolutionDir) $(SolutionDir)subwcrev_template.txt $(SolutionDir)version.h

I have little control over where other developers will place the solution, so I had to support spaces in the path. Trying to use quotes around paths to support spaces, I came up with the following command. It always fails.

subwcrev "$(SolutionDir)" "$(SolutionDir)subwcrev_template.txt" "$(SolutionDir)version.h"

Almost by accident, I found the solution, put a space between the last character of the path and the double quote.

subwcrev "$(SolutionDir) " "$(SolutionDir)subwcrev_template.txt " "$(SolutionDir)version.h "

This worked. I tested this in AVR Studio 6.1, which uses a Visual Studio Shell.

Solution 2

Visual Studio project files are XML files. Some special characters, such as the double quote, have to be escaped by using named entities. I think they're similar to what's used for encoding strings to html.

MSDN has a reference on How To Use Reserved XML Characters in Project Files. In your example, all you would need to do to accomplish your copy is this in the .csproj/.vbproj file:

copy "$(SolutionDir)$(Configuration)\*" "$(TargetDir)"

That will wrap both paths in double quotes. You'll get errors when referencing paths with spaces and that's why the double quotes are required.

Solution 3

You need to put a double quote within two double quotes.

Example of a copy file in a post build step: copy /Y """C:\source path with spaces\somefile.txt""" """C:\destination path with spaces\"""

Solution 4

I couldn't get the other answers to work.

I finally just escaped the last "\":


Solution 5

Please, oh please don't use post build events.
Instead, use the power of MSBuild's AfterBuild target:

Right click on your project and select Edit Project File. Add an AfterBuild event:

  <Target Name="AfterBuild">
      <FilesToCopy Include="$(SolutionDir)$(Configuration)\*" />
    <Copy SourceFiles="@(FilesToCopy)"
          OverwriteReadOnlyFiles="true" SkipUnchangedFiles="false" Condition="'@(FilesToCopy)' != ''" />

Unlike the PostBuildEvent which executes by raw cmd.exe, BeforeBuild/AfterBuild targets run by managed code, which ensures more robust execution, better maintainability and traceability.


Related videos on Youtube

Author by


Software Engineer for over 35 years. C++ fluent. C# newbie

Updated on September 24, 2022


  • JonN
    JonN 2 months

    I'm trying to create a post build file copy step in VS2010 which handles path macros when they have embedded spaces. I've tried surrounding the copy commands in double quotes but I get error from when copy is invoked if $(SolutionDir) contains a space. the echoed command line in the error message does not show the double quotes.

    copy "$(SolutionDir)$(Configuration)\*" "$(TargetDir)"

    I also tried separately \" and "" but both of these cause the 2 character escape sequence to appear in the echoed command line? How does one properly escape a double quote in a build step?

  • Mrchief
    Mrchief over 8 years
    Does this take care of spaces in path?
  • KMoraz
    KMoraz over 8 years
    @Mrchief Yes. Spaces can also be escaped using '%22'
  • Mrchief
    Mrchief over 8 years
    Not sure how... Assume $(SolutionDir) expands to C:\Path With Spaces\src. Care to show an example?
  • KMoraz
    KMoraz over 8 years
    Then %22$(SolutionDir)%22 will become "C:\Path With Spaces\src". For the example above you don't need to escape spaces as Copy task takes care of it.
  • Ali
    Ali over 7 years
    I could not understand the reason of escaping the double quote but it worked.
  • Thomas
    Thomas over 7 years
    you can also escape the quotes (") with &quot; in XML
  • Rick Love
    Rick Love almost 7 years
    Very rarely, we software developers have to do something just because it works. It makes you feel dirty without a logical reason to do so. But in this case, it is due to a design limitation (aka a bug). Hopefully MS will fix this in the future.
  • Søren Boisen
    Søren Boisen almost 4 years
    This doesn't work in VS2017 - "'quot' is not recognized as an internal or external command"
  • Søren Boisen
    Søren Boisen almost 4 years
    Sweet, in VS2017 this suggestion worked whereas escaping with backslash or triple quotes or using XML entities failed utterly.
  • Jeff LaFay
    Jeff LaFay almost 4 years
    @SørenBoisen this answer is specific to VS2010.
  • Matt Arnold
    Matt Arnold about 3 years
    @Ali The \ isn't escaping the double quote but rather unescaping the prior trailing \ from $(TargetDir) so that it isn't interpreted as an escape character itself for the double quote and instead as a normal independent backslash.
  • Matt Arnold
    Matt Arnold about 3 years
    Just to add to my previous comment, unfortunately using the C# verbatim syntax of @ before the string, does not make \ a literal character in this context and instead just includes the @ in the string!
  • Vedran
    Vedran almost 3 years
    It's because "$(SolutionDir)" resolves to "some folder\" thus the escaping your quote; "$(SolutionDir)\" will work though.