• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

Need a script that deletes empty folders

Do you have a script, a DOS command line or a VB script, that will cycle through a folder and delete empty sub-folders?
5 Solutions
Glary Utilities will remove empty folders on your computer and it is free.  Hope that works.
RD in dos without parameters will only remove empty folders.

Take a list of directories reverse sort the list and begin removing.

Save the code as RemoveEmptyFolders.cmd

On a command line run.

RemoveEmptyFolders "C:\Windows"
if exist "%~1" for /f "tokens=*" %%a in ('dir "%~1" /s /b /ad ^| sort /r') do RD "%%a"

Open in new window

Steve KnightIT ConsultancyCommented:
I was going to suggest something along the lines of the batch script Amzingtech has already given you above (which works fine for me) so the only thing I would add is you could give some feedback, e.g.:

@echo off
if exist "%~1" for /f "tokens=*" %%a in ('dir "%~1" /s /b /ad ^| sort /r') do TITLE Working on "%%a" & RD "%%a" 2>NUL && echo Removing %%a

So it will hide all the "errors" about not being able to remove the non-empty dirs, will show you progress in the title bar of the cmd window, and will show which directories ARE removed using the &&echo at the end.


Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now