"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.
'————————————————————— 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?
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.