Removing empty directories with msbuild

How can I process the path recursively so that after the processing of empty directories is not in the path.

For example, let's say that we have C:\Dir1\Dir2\Dir3 , and there are no files in any of these directories. The result should be the removal of all three directories.

I would like to accomplish this without using custom tasks.

+6
source share
2 answers

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> 
+12
source

Using Exec Task with 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" } 

Provided by Guy Ellis Rocks: Powershell script to remove empty directories

+3
source

Source: https://habr.com/ru/post/918181/


All Articles