Copy entire directory to output folder maintaining the folder structure?

Visual StudioVisual Studio-2012DirectoryCopyXcopy

Visual Studio Problem Overview


I want a specific directory to be copied to output folder ("bin") on every build. I think it can be handled via post build scripts. But I'm not sure how to copy a directory itself. I know how to handle specific files.

For eg, this works for a file:

In

> Project > Properties > Build Events> Post Build

COPY "$(SolutionDir)Resources\Release Notes.pdf" "$(TargetDir)"

But suppose I have a directory Template, now I need everything under Template to come to bin folder upon successful build maintaining the folder structure.

I tried this:

COPY "$(SolutionDir)Resources\Template\" "$(TargetDir)"

Only the files in Template directory gets copied this way and not the sub directories and the files inside Template folder. I want the folder Template itself to come inside my output bin folder. In other words, bin should look like:

bin > Template > abc.xxx  
                 xxx.yyy
                 Subdirectory1 > asd.qwe
                                 zxc.qwe 
                 Subdirectory2 > ...

This could be a duplicate, but I couldn't find a relevant thread. Thanks.

Visual Studio Solutions


Solution 1 - Visual Studio

I just added this to my *.csproj file (right click Edit Project File)

<ItemGroup>
    <Content Include="MYCUSTOMFOLDER\**">
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
</ItemGroup>

I think for this the directory needs to be on same hierarchy level as *.csproj file or bellow that.

Solution 2 - Visual Studio

This worked for me. /S is the key which copies everything recursively.

XCOPY "$(SolutionDir)Resources\Template" "$(TargetDir)\Template\" /S

Since I wanted files to be overwritten every time without a prompt, I added a /Y switch as well.

XCOPY "$(SolutionDir)Resources\Template" "$(TargetDir)\Template\" /S /Y

Solution 3 - Visual Studio

Try XCOPY instead of COPY; e.g.

XCOPY "$(SolutionDir)Resources\Template\" "$(TargetDir)\Template" /s /i /y

More info on XCOPY here...

http://www.computerhope.com/xcopyhlp.htm

Solution 4 - Visual Studio

Here's an additional solution working on Visual Studio 2019 as of the date of this post. This will copy the folder structure recursively and all files within. Tested on a C++ .vcxproj in a multi-project solution.


First, start by editing your [ .proj / .vcxproj / .csproj ] file. Once open, find your project scoped tag. If you already have ItemGroups within, then paste the code below directly after the existing ones. Otherwise, add it in before the PropertyGroup tags. Then modify the Include & Link parameters for the folder structure you wish to copy to the output path.

<ItemGroup>
	<Content Include="..\Assets\**\*.*">
 		<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
		<DeploymentContent>true</DeploymentContent>
		<Link>Assets\%(RecursiveDir)\%(Filename)%(Extension)</Link>
	</Content>
</ItemGroup>

Note: If you have multiple top level folders, like JS, IMG, BIN, etc., then create a new entry for each one.

Solution 5 - Visual Studio

I have a working solution of this question:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
	<ItemGroup>
		<CommonFont Include="..\common\src\Web\wwwroot\css\fonts\*.*" />
	</ItemGroup>
	<Copy SourceFiles="@(CommonFont)"  DestinationFolder="wwwroot\css\fonts" SkipUnchangedFiles="true" />
</Target>

Solution 6 - Visual Studio

The solution by CodingYourLife almost worked for me, but I found out that PreserveNewest was not being respected. I found a solution on the Visual Studio forums that works correctly. My .CSPROJ now looks like this:


    <Content Include="assets\**">
        <Link>assets\%(RecursiveDir)\%(Filename)%(Extension)</Link>
        <TargetPath>assets\%(RecursiveDir)\%(Filename)%(Extension)</TargetPath>
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>

Note: This solution requires Visual Studio 16.10 or newer.

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
QuestionnawfalView Question on Stackoverflow
Solution 1 - Visual StudioCodingYourLifeView Answer on Stackoverflow
Solution 2 - Visual StudionawfalView Answer on Stackoverflow
Solution 3 - Visual StudioPhilAIView Answer on Stackoverflow
Solution 4 - Visual StudioZannaView Answer on Stackoverflow
Solution 5 - Visual Studiouser1575120View Answer on Stackoverflow
Solution 6 - Visual StudiovandreView Answer on Stackoverflow