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>
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>
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)"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With