Question

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.

Was it helpful?

Solution 2

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

OTHER TIPS

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.

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

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.

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>

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top