Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all file and folders with msbuild

Tags:

msbuild

How can I delete all files and folders from a given path?

I tried this, but I'm unable to select the directories.

<Target Name="CleanSource" Condition="$(path)!=''">      <Message Text="path=$(path)"/>      <ItemGroup>       <fileToDelete Include="$(path)\**\*.*" />       <directoryToDelete Include="$(path)\**\" /><!these doest not select any directory at all-->          </ItemGroup>      <Message Text="file to delete:@(fileToDelete)"/>     <Message Text="directory to delete:@(directoryToDelete)"/>      <Delete Files="@(fileToDelete)" />     <Message Text="file effectively deleted:@(DeletedFiles)"/>     <RemoveDir Directories="@(directoryToDelete)" />     <Message Text="Directory effectively deleted:@(RemovedDirectories)"/>  </Target> 
like image 674
Pitming_Reloaded Avatar asked Feb 22 '11 16:02

Pitming_Reloaded


2 Answers

The RemoveDir task removes the specified directories and all of its files and subdirectories. You don't have to remove the files and subdirectories first. Just pass the directory name to RemoveDir.

   <ItemGroup>         <DirsToClean Include="work" />     </ItemGroup>     <Target Name="CleanWork">         <RemoveDir Directories="@(DirsToClean)" />     </Target> 
like image 168
Brian Walker Avatar answered Sep 29 '22 17:09

Brian Walker


While there are ways to construct this using just MSBuild, I'd highly recommend the MSBuild Extension pack.

http://msbuildextensionpack.codeplex.com/ [has been moved]
GitHub: MSBuildExtensionPack

Using the pack, you get a RemoveContent task that does exactly what you are needing. Once you install, you'd just do something like:

<MSBuild.ExtensionPack.FileSystem.Folder    TaskAction="RemoveContent" Path="$(PathtoEmpty)"/> 
like image 26
Taylor Bird Avatar answered Sep 29 '22 18:09

Taylor Bird