Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatic MSBuild Recursive Copy with wildcards

Tags:

c#

msbuild

I am creating an MSBuild v4 task that happens to need to call the Copy task to recursively copy some files (without flattening the directory structure at the destination).

I have come up with:

var copy = new Microsoft.Build.Tasks.Copy
{
    BuildEngine = this.BuildEngine,
    SourceFiles = new ITaskItem[] { new TaskItem(@"C:\source\**\*.foo") },
    DestinationFolder = new TaskItem(@"c:\dest\\")
};
copy.Execute();

but am getting an error 'Could not copy C:\source\**\*.foo to c:\dest\* - Illegal characters in path'

There doesn't seem to be much online help for pragmatic invocation, and have drawn a blank. Any ideas?

Thanks

Jon

like image 766
Jon Bates Avatar asked Dec 04 '22 06:12

Jon Bates


1 Answers

It looks like the Copy task doesn't have an intrinsic understanding of recursion; the following code would cause the Copy task to be invoked once per file level, and this is handled by the MSBuild runner.

<ItemGroup>
  <x Include="c:\source\**\*.foo" />
</ItemGroup>
<Copy SourceFiles="@(x)" DestinationFolder="c:\dest\%(RecursiveDir)" />

However, since the Copy task seems to treat SourceFiles and DestinationFiles as an associative array (each of type ITaskItem[]), we just performed a recursive descent and built up these two arrays manually, before execing it

like image 142
Jon Bates Avatar answered Dec 15 '22 07:12

Jon Bates