Friday, 20 February 2009

MsBuild: How to copy/delete all files from subfolders

Copying entire folder content (with subfolders) using standard MsBuild is not as straightforward as I'd imagine this to be. The only solution I've found uses recursion. Following example copies all files from source to dest folder:

C:\source
C:\dest


<AllFolderFiles Include="$(SourceDir)\**\*.*" />

<Copy SourceFiles="@(AllFolderFiles)"
DestinationFiles="@(AllFolderFiles -> $(DestinationDir)\
%(RecursiveDir)%(Filename)%(Extension)')" />

You can also use recursion to delete all files in folder and its subfolders:

C:\source


<FilesToDelete Include="$(ToDelete)\**\*.*" />

<Delete Files="@(FilesToDelete)" />

Anybody knows easier solution?

1 comment:

Anonymous said...

One problem I see with your DELETE fucntion is, it deletes all the files recursively but does not delete the folders recursively. You would still have the folder structure.