Remove empty directories using msbuild

msbuild

Solution

Something like this should work, didn't check the performance of counting thousands of files though just to get array length...

<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>

Problem

How can I process a path recursively so that once the processing completes there are no empty directories under the path. For example, say we have `C:\Dir1\Dir2\Dir3` and there are no files in any of these directories. The outcome should be the removal of all three directories. I would like to accomplish this without the use of custom tasks.

Original source