Something like this should work, not check the performance of counting thousands of files, but just get the length of the array ...
<Project DefaultTargets="Foo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Target Name="Foo"> <ItemGroup> <Directories Include="$([System.IO.Directory]::GetDirectories('D:\foo', '*', System.IO.SearchOption.AllDirectories))" /> <Directories> <Files>$([System.IO.Directory]::GetFiles("%(Directories.Identity)", "*", System.IO.SearchOption.AllDirectories).get_Length())</Files> </Directories> </ItemGroup> <RemoveDir Directories="@(Directories)" Condition="%(Files)=='0'" /> </Target> </Project>
source share