Delete empty folders (one-click function)

Here you can propose new features, make suggestions etc.

Moderators: white, Hacker, petermad, Stefan2

User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Delete empty folders (one-click function)

Post by *beb »

PowerShell_DeletEmptyFolders_Display_Log_Remove.ps1 [updated]

Code: Select all

"creating folders structure [needed only for the testing purposes]:"
$md = '.\empty\empty'    ;New-Item $md -force -Type directory         >$null; $md
$md = '.\empty\folder'   ;New-Item $md -force -Type directory         >$null; $md
$md = '.\empty\structure';New-Item $md -force -Type directory         >$null; $md
$md = '.\empty\file.txt' ;New-Item $md -force -Type file -value "file">$null; $md
# feel free to remove the above code as soon as you make sure the script is working as intended

"";"working directory:";([System.Environment]::CurrentDirectory)

"";"empty folders that can be deleted:"
$stamp = (Get-Date -format "yyyyMMdd_HHmmssf");$log = $($stamp+'_del.log')
Get-ChildItem (Get-Location) -force -recurse|Where {$_.PSIsContainer -and @(Get-ChildItem -literalPath $_.Fullname -force -recurse|Where {!$_.PSIsContainer}).Length -eq 0}|Select-Object -ExpandProperty FullName|Out-File $log
Get-Content($log)

"";"empty folders actually being deleted:"
Get-ChildItem (Get-Location) -force -recurse|Where {$_.PSIsContainer -and @(Get-ChildItem -literalPath $_.Fullname -force -recurse|Where {!$_.PSIsContainer}).Length -eq 0}|Remove-Item -force -recurse -Verbose -ErrorAction Ignore 4>&1|Foreach-Object {Write-Host ($_.Message -replace '(.*)"(.*)"(.*)','$2') -ForegroundColor Yellow}

"";"see deleted folders list in a log file:";$log

"";pause
Note: The updated version addresses the excessive Remove-Item -Verbose output in a way to display only the full paths of the empty directories that are getting deleted.

[modified] Example script output:
https://i.imgur.com/qvvauSe.png
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
oko
Senior Member
Senior Member
Posts: 201
Joined: 2007-05-03, 16:22 UTC

Re: Delete empty folders (one-click function)

Post by *oko »

Fla$her wrote: 2022-05-25, 13:17 UTC
oko wrote: 2022-05-25, 08:24 UTCI would like to request in vbs:

Code: Select all

'————————————————————— VBS —————————————————————
' Recursive deletion of empty subdirectories
' Parameter: "<folder path>" (for example, "%P")
'———————————————————————————————————————————————
Option Explicit: Dim Dir, FSO, Col, D, c, Msg
Dir = WSH.Arguments(0)
Const Title = " Deleting empty subfolders"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Col = CreateObject("System.Collections.ArrayList")
If Not FSO.FolderExists("\\?\" & Dir) Then _
MsgBox "Set the path to the existing directory!", 262192, Title: Quit
If MsgBox("Execute in """ & Dir & """?", 262180, Title) = 7 Then Quit
Recursion FSO.GetFolder("\\?\" & Dir)
Col.Sort : Col.Reverse
For Each D in Col
   Set D = FSO.GetFolder(D)
   If D.SubFolders.Count + D.Files.Count = 0 Then c = c + 1: D.Delete 1
Next
If c Then Msg = "  Done!" & vbLf &_
"  Count of deleted: " & c Else Msg = "No empty subfolders found."
MsgBox Msg, 262208, Title: Quit
Sub Quit : Set FSO = Nothing : Set Col = Nothing : WSH.Quit : End Sub

Sub Recursion(oDir)
  For Each D in oDir.SubFolders
    If D.SubFolders.Count Then Col.Add D.Path :_
    Recursion D Else If D.Files.Count = 0 Then c = c + 1: D.Delete 1
  Next
End Sub
I still use this script for quick deletion of empty folders. It works well, but it can not work always. I found out it does not work if the space is in path of initial directory. The "Set the path to the existing directory!" is shown. I run it from TC Start menu where Command is vbs file and Parameters is %P. Can you help me with it?
User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: Delete empty folders (one-click function)

Post by *beb »

My current version of the command (to delete empty folders under the active pane) is as follows:
For ancient versions of PowerShell (like 5.1)

Code: Select all

[em_delEmptyDirs]
cmd=powershell -command
param=Get-ChildItem $pwd -force -recurse -EA Silently|Where {$_.PSIsContainer -and @(Get-ChildItem -literal $_.Fullname -force -recurse -EA Silently|Where {!$_.PSIsContainer}).Length -eq 0}|Tee-Object -variable array|Remove-Item -force -recurse -EA Silently;$array|ForEach {Write-Host $_ -f Yellow};sleep -s 3
For modern versions of PowerShell (like 7.4)

Code: Select all

[em_delEmptyDirs]
cmd=pwsh -command
param=Get-ChildItem $pwd -force -recurse -EA Silently|Where {$_.PSIsContainer -and @(Get-ChildItem -literal $_.Fullname -force -recurse -EA Silently|Where {!$_.PSIsContainer}).Length -eq 0}|Tee-Object -variable array|Remove-Item -force -recurse -EA Silently;$array|ForEach {Write-Host $_ -f Yellow};sleep -s 3
It proved to be 100% effective, with no known flaws.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: Delete empty folders (one-click function)

Post by *Fla$her »

oko wrote: 2024-01-31, 21:01 UTC

Code: Select all

' Parameter: "<folder path>" (for example, "%P")
and Parameters is %P. Can you help me with it?
<< :?: >>
for example, "%P"
Overquoting is evil! 👎
oko
Senior Member
Senior Member
Posts: 201
Joined: 2007-05-03, 16:22 UTC

Re: Delete empty folders (one-click function)

Post by *oko »

2Fla$her
yes, quotation marks missing. I did try with them but it did not work, now I just tried again and it works. Maybe I had combined more errors which led me to the wrong conclusions. My mistake. Solved.
Post Reply