Search code examples
visual-studiovisual-studio-2012directorycopyxcopy

Copy entire directory to output folder maintaining the folder structure?


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.


Solution

  • 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