30

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>
Darko
  • 38,310
  • 15
  • 80
  • 107
Pitming_Reloaded
  • 541
  • 2
  • 5
  • 12
  • 1
    possible duplicate of [Msbuild - how to delete folder contents but not folder itself?](http://stackoverflow.com/questions/15109785/msbuild-how-to-delete-folder-contents-but-not-folder-itself) – oɔɯǝɹ Sep 29 '15 at 14:45

4 Answers4

77

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>
SomeInternetGuy
  • 1,007
  • 12
  • 19
Brian Walker
  • 8,658
  • 2
  • 33
  • 35
  • Thx, but the param I get is a path and i just want all directory under these path to be deleted. Not the files in the root of that path. – Pitming_Reloaded Feb 22 '11 at 16:37
  • 13
    Your original question says "delete all file and folders from a given path". If your requirement is different you should state that in the question. To only delete the folders from a directory, leaving the files, you will need a custom task. If your naming scheme is such that you could create an ItemGroup of the folder names, then you could pass that ItemGroup to RemoveDir to delete them. – Brian Walker Feb 22 '11 at 17:58
  • I don't want to delete the folder, just the contents of the folder. – IAbstract Mar 17 '16 at 17:09
16

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)"/>
IAbstract
  • 19,551
  • 15
  • 98
  • 146
Taylor Bird
  • 7,767
  • 1
  • 25
  • 31
-2

I'm arriving to this conversation a little late, but I found the easiest way to accomplish this was to use the Exec task to execute the batch command given by lain in response to a similar question (with minor edits by yours truly):

<Exec Command="FOR /D %%p IN (&quot;$(path)*.*&quot;) DO rmdir &quot;%%p&quot; /s /q" />
Community
  • 1
  • 1
-22

Finally I did use powershell wich is much more fast:

<exec>
 <executable>powershell.exe</executable>
 <buildArgs><![CDATA[-command "& {if( [System.IO.Directory]::Exists($pwd) ){dir $pwd | ri -recurse
    -force}}"]]></buildArgs>
</exec>
Pitming_Reloaded
  • 541
  • 2
  • 5
  • 12