如何使用msbuild删除所有文件和文件夹

问题描述:

如何从给定路径删除所有文件和文件夹?

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>

最后,我确实使用powershell wich快得多:

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>