How To: Recursively Copy Files Using the Task
Have you ever run into a situation where you had to recursively copy files from one folder into another as a part of your build process? We certainly have run into it a few times, and I thought I'd share with you a neat trick that involves the use of metadata and the Copy task.
Before you can copy a set of files, you need to be able to recursively add those files to an item list. Here's how you do that when declaring items.
<
ItemGroup>
<Compile Include=".\**\*.cs" />
</ItemGroup>
The ** wildcard is used in item includes to indicate recursive includes.
So, once you have an item declared as such, you can rely on a piece of standard meta-data that goes with all items (i.e. RecursiveDir) to accomplish your recursive copy. The RecursiveDir metadata when used will return the evaluated value of the ** metadata for each item - and you can use that value to preserve the folder structure for each file when performing the copy. Here's how you invoke the <Copy> task using this piece of metadata to copy recursively.
<
Copy SourceFiles="@(Compile)" DestinationFolder="c:\foocopy\%(RecursiveDir)"></Copy>
Hope this helps! Let us know if these tips/tricks and how-tos are useful.
[ Author: Faisal Mohamood ]