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

前端 未结 2 1482
挽巷
挽巷 2021-01-17 10:32

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


However I\'d rather delete the co

相关标签:
2条回答
  • 2021-01-17 11:15

    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 *
    
    0 讨论(0)
  • 2021-01-17 11:17

    Download and install the msbuild extension pack then use

    <MSBuild.ExtensionPack.FileSystem.Folder TaskAction="RemoveContent" Path="$(Bin)" />
    
    0 讨论(0)
提交回复
热议问题