How to delete all file and folders with msbuild
Asked Answered
C

4

32

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>
Counterrevolution answered 22/2, 2011 at 16:24 Comment(1)
possible duplicate of Msbuild - how to delete folder contents but not folder itself?Friedlander
C
-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>
Counterrevolution answered 28/2, 2011 at 12:48 Comment(4)
Using Powershell is not what was expected here.Irresponsive
This is not really deleting the folder "with msbuild", as the original question was phrased.Psychosurgery
This fulfills an interesting criteria: it allows you to add a -Force flagAdown
@Izzy: OP simply accepted own solution regardless of any practical answers provided.Mulvihill
K
79

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>
Kee answered 22/2, 2011 at 16:31 Comment(3)
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.Counterrevolution
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.Kee
I don't want to delete the folder, just the contents of the folder.Mulvihill
B
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)"/>
Belkisbelknap answered 22/2, 2011 at 16:29 Comment(0)
P
-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" />
Precocious answered 22/4, 2013 at 4:10 Comment(2)
Your solution deletes the main folder too.Thromboplastin
This is pretty hacky as it is not platform independent any longer.Antitragus
C
-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>
Counterrevolution answered 28/2, 2011 at 12:48 Comment(4)
Using Powershell is not what was expected here.Irresponsive
This is not really deleting the folder "with msbuild", as the original question was phrased.Psychosurgery
This fulfills an interesting criteria: it allows you to add a -Force flagAdown
@Izzy: OP simply accepted own solution regardless of any practical answers provided.Mulvihill

© 2022 - 2024 — McMap. All rights reserved.