Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove empty directories using msbuild

Tags:

msbuild

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.

like image 563
Jason Kresowaty Avatar asked Jun 15 '12 13:06

Jason Kresowaty


People also ask

How do you remove multiple empty directories?

Delete Empty Files in a Directory First, search all the empty files in the given directory and then, delete all those files. This particular part of the command, find . -type f -empty -print, will find all the empty files in the given directory recursively. Then, we add the -delete option to delete all those files.

Which command is used to remove empty directories?

Use the rmdir command to remove the directory, specified by the Directory parameter, from the system. The directory must be empty (it can contain only . and ..) before you can remove it, and you must have write permission in its parent directory.

Can rm remove empty directories?

The rm command removes complete directories, including subdirectories and files. The rmdir command removes empty directories.

How do I remove an empty directory in git?

Use rm -r switch with the git command to remove directory recursively. After removing the directory you need to commit changes to the local git repository. Then push the changes to remove the directory from the remote git repository.


2 Answers

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>
like image 58
Ilya Kozhevnikov Avatar answered Sep 23 '22 02:09

Ilya Kozhevnikov


Using an Exec Task running PowerShell:

MSBuild

<Project DefaultTargets="DefaultTarget" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <CleanPath>C:\MyDirAboveDir1</CleanPath>
    </PropertyGroup>

    <Target Name="DefaultTarget">
        <Exec Command="PowerShell .\RemoveDir.ps1 '$(CleanPath)'" />
    </Target>
</Project>

PowerShell RemoveDir.ps1

Param ([string]$folderpath = $(throw "provide folderpath"))

$folders = Get-ChildItem $folderpath -recurse -force | ? {$_.PSIsContainer}

if ($folders -ne $null)
{
    [array]::Reverse($folders)

    foreach($folder in $folders)
    {
        Write-Host "Examining contents of $($folder.fullname)"
        $childitems = Get-Childitem $folder.fullname -recurse -force | ? { $_.PSIsContainer -eq $false }

        if($childitems -eq $null)
        {
            "Remove folder: " + $folder.FullName
            Remove-Item $folder.FullName -Recurse -Force
        }
        else
        {
            Write-host "Files found in $folder, skipping delete"
        }

        $childitems = $null
    }
}
else
{
    Write-Host "no sub folders found"
}

Courtesy to Guy Ellis Rocks: Powershell script to remove empty directories

like image 20
Filburt Avatar answered Sep 20 '22 02:09

Filburt