Batch Command To Delete Only Empty Subfolders


Follow-up: I asked co-pilot how to do this and it gave me the below powershell script. I want to stress that I have not tested this, but the claim is that this code will do what you are looking for. Test it on unimportant data first!
Once again, AI is great at spewing code. Terrible at requirements (because AI doesn't do test runs!).
Suppose your naive user points this to C:\Windows.

Code:
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\com\dmp".
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\drivers\UMDF\en-US".
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\DriverStore\FileRepository".
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\LogFiles\Windows Portable Devices".
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\migwiz\dlmanifests\Microsoft-Windows-IE-ESC".
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\migwiz\dlmanifests\Microsoft-Windows-RasApi".
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\MUI\dispspec".
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\oobe\en-US".
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\Setup\en-US".
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\sysprep\en-US".
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\wbem\AutoRecover".
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\wbem\Logs".
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\wbem\Repository".
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\wbem\tmf".
What if: Performing the operation "Remove Directory" on target "C:\Windows\SysWOW64\WindowsPowerShell\v1.0\SessionConfig".
What if: Performing the operation "Remove Directory" on target "C:\Windows\Vss\Writers\Application".
What if: Performing the operation "Remove Directory" on target "C:\Windows\winsxs\amd64_microsoft-windows-baseapinamespace_31bf3856ad364e35_6.1.7601.17514_none_a4272f399040a523".
What if: Performing the operation "Remove Directory" on target "C:\Windows\winsxs\amd64_microsoft-windows-d..evelapisets-windows_31bf3856ad364e35_7.1.7601.16492_none_e249fd3fed68cb81".
What if: Performing the operation "Remove Directory" on target "C:\Windows\winsxs\amd64_microsoft-windows-downlevelapisets-base_31bf3856ad364e35_7.1.7601.16492_none_1ed670cbaddb31b7".
What if: Performing the operation "Remove Directory" on target "C:\Windows\winsxs\amd64_microsoft-windows-downlevelapisets-com_31bf3856ad364e35_7.1.7601.16492_none_5b1161f912e23f6d".
What if: Performing the operation "Remove Directory" on target "C:\Windows\winsxs\amd64_microsoft-windows-downlevelapisets-shell_31bf3856ad364e35_7.1.7601.16492_none_2b20f882c1c0eaca".
What if: Performing the operation "Remove Directory" on target "C:\Windows\winsxs\amd64_microsoft-windows-minioapinamespace_31bf3856ad364e35_6.1.7600.16385_none_c8b8ba7bcb4e2c66".

I hope you have backups...
 
Last edited:

My Computer

System One

  • OS
    Windows 7
Therein lies the challenge. I think, for safety, an exclusion (inclusion?) list is the correct way to do it.

Code:
if ($args[0] -eq '') {
   exit
}

$Path = $args[0]

if (-not (Test-Path $Path)) {
   "`"$Path`" folder doesn't exist."
   exit
}

$ExcludeList = @(
    'desktop.ini'
    'Thumbs.db'
)

$ListSize = $ExcludeList.Count

@(Get-ChildItem $Path -Recurse -Directory -Force -ErrorAction SilentlyContinue; Get-Item $Path) | Sort-Object -Property FullName -Descending | ForEach-Object {
    $Folder = $_.FullName
    $Children = Get-ChildItem $Folder -Force -ErrorAction SilentlyContinue

    if (($Children | Select-Object -First 1).Count -eq 0) {
        $Folder
    }
    else {
        if ($Children.Count -le $ListSize -and ($Children | Where-Object { $_ -notin $ExcludeList }).Count -eq 0) {
            # If we don't do a Remove-Item on excluded files, the outside loop Remove-Item complains it needs to be recursive to clean up this folder.  That would introduce new logic flaws...
            Remove-Item -Force $($Children | Where-Object { $_ -in $ExcludeList }).Fullname
            $Folder
        }
    }
} | Remove-Item -Force -Verbose
 
Last edited:

My Computer

System One

  • OS
    Windows 7

Latest Support Threads

Back
Top Bottom