Visual Studio Post Build Event - Copy to Relative Directory Location

Visual StudioVisual Studio-2008Post Build-Event

Visual Studio Problem Overview


On a successful build, I wish to copy the contents of the output directory to a different location under the same "base" folder. This parent folder is a relative part and can vary based on Source Control settings.

I have listed a few of the Macro values available to me ...

$(SolutionDir) = D:\GlobalDir\Version\AppName\Solution1\build

$(ProjectDir) = D:\GlobalDir\Version\AppName\Solution1\Version\ProjectA\

I want to copy the Output Dir contents to the following folder :

D:\GlobalDir\Version\AppName\Solution2\Project\Dependency

The base location "D:\GlobalDir\Version\AppName" needs to be fetched from one of the above macros. However, none of the macro values list only the parent location.

How do I extract only the base location for the post build copy command ?

Visual Studio Solutions


Solution 1 - Visual Studio

Here is what you want to put in the project's Post-build event command line:

copy /Y "$(TargetDir)$(ProjectName).dll" "$(SolutionDir)lib\$(ProjectName).dll"

EDIT: Or if your target name is different than the Project Name.

copy /Y "$(TargetDir)$(TargetName).dll" "$(SolutionDir)lib\$(TargetName).dll"

Solution 2 - Visual Studio

If none of the TargetDir or other macros point to the right place, use the ".." directory to go backwards up the folder hierarchy.

ie. Use $(SolutionDir)\..\.. to get your base directory.


For list of all macros, see here:

http://msdn.microsoft.com/en-us/library/c02as0cs.aspx

Solution 3 - Visual Studio

You could try:

$(SolutionDir)..\..\

Solution 4 - Visual Studio

I think this is related, but I had a problem when building directly using msbuild command line (from a batch file) vs building from within VS.

Using something like the following:

<PostBuildEvent>
  MOVE /Y "$(TargetDir)something.file1" "$(ProjectDir)something.file1"
  start XCOPY /Y /R "$(SolutionDir)SomeConsoleApp\bin\$(ConfigurationName)\*" "$(ProjectDir)App_Data\Consoles\SomeConsoleApp\"
</PostBuildEvent>

(note: start XCOPY rather than XCOPY used to get around a permissions issue which prevented copying)

The macro $(SolutionDir) evaluated to ..\ when executing msbuild from a batchfile, which resulted in the XCOPY command failing. It otherwise worked fine when built from within Visual Studio. Confirmed using /verbosity:diagnostic to see the evaluated output.

Using the macro $(ProjectDir)..\ instead, which amounts to the same thing, worked fine and retained the full path in both build scenarios.

Solution 5 - Visual Studio

Would it not make sense to use msbuild directly? If you are doing this with every build, then you can add a msbuild task at the end? If you would just like to see if you can’t find another macro value that is not showed on the Visual Studio IDE, you could switch on the msbuild options to diagnostic and that will show you all of the variables that you could use, as well as their current value.

To switch this on in visual studio, go to Tools/Options then scroll down the tree view to the section called Projects and Solutions, expand that and click on Build and Run, at the right their is a drop down that specify the build output verbosity, setting that to diagnostic, will show you what other macro values you could use.

Because I don’t quite know to what level you would like to go, and how complex you want your build to be, this might give you some idea. I have recently been doing build scripts, that even execute SQL code as part of the build. If you would like some more help or even some sample build scripts, let me know, but if it is just a small process you want to run at the end of the build, the perhaps going the full msbuild script is a bit of over kill.

Hope it helps Rihan

Solution 6 - Visual Studio

I solved my problem by reinstall VS and then download .Net Core (3.x and 2.x) sdk packages

Solution 7 - Visual Studio

Here is my post build script

  1. Creates the custom path for my own. Including library and version.
  2. Copies the .dll (target file)
  3. Copies the *.md files.

script:

md c:\_References\$(ProjectName)\$(AssemblyVersion)
xcopy $(ProjectDir)$(OutDir)$(TargetFileName) c:\_References\$(ProjectName)\$(AssemblyVersion) /y
xcopy $(ProjectDir)*.md c:\_References\$(ProjectName)\$(AssemblyVersion) /y

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPreetsView Question on Stackoverflow
Solution 1 - Visual StudioLucas BView Answer on Stackoverflow
Solution 2 - Visual StudiogbjbaanbView Answer on Stackoverflow
Solution 3 - Visual StudioichibanView Answer on Stackoverflow
Solution 4 - Visual StudiodrzausView Answer on Stackoverflow
Solution 5 - Visual StudioRihan MeijView Answer on Stackoverflow
Solution 6 - Visual StudioFelipe DinizView Answer on Stackoverflow
Solution 7 - Visual StudioOzan BAYRAMView Answer on Stackoverflow