Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Msbuild - how to delete folder contents but not folder itself?

Tags:

msbuild

Right now in my msbuild script is a task to delete a folder

<RemoveDir Directories="$(Bin)"/>

However I'd rather delete the contents of the folder but leave the folder be (in case someone has the folder open in Windows Explorer). How can I do that?

like image 989
Colonel Panic Avatar asked Feb 27 '13 10:02

Colonel Panic


People also ask

How do I disband a folder?

Right-click the folder you want to delete and click Delete Folder. Click Yes to move the folder and its contents to the Deleted Items folder. When you empty the Deleted Items folder, everything in it — including any folders you've deleted — is permanently erased.


1 Answers

This will remove all files and subfolders:

    <Target Name="CleanFolder">

    <PropertyGroup>
      <TargetFolder>c:\clean</TargetFolder>
    </PropertyGroup>

    <ItemGroup>
      <FilesToClean Include="$(TargetFolder)\**\*"/>
      <Directories Include="$([System.IO.Directory]::GetDirectories('$(TargetFolder)', '*', System.IO.SearchOption.AllDirectories))"
                   Exclude="$(TargetFolder)"/>
    </ItemGroup>

    <Delete Files="@(FilesToClean)" ContinueOnError="true"/>
    <RemoveDir Directories="@(Directories)" />
  </Target>

It would also be good to drop open connections using the openfiles tool:

openfiles /disconnect /ID *
like image 119
James Woolfenden Avatar answered Nov 10 '22 01:11

James Woolfenden