Need a script that deletes empty folders

Posted on 2010-01-04
Last Modified: 2012-05-08
Do you have a script, a DOS command line or a VB script, that will cycle through a folder and delete empty sub-folders?
Question by:gtrapp
    LVL 59

    Assisted Solution

    by:Darius Ghassem
    LVL 10

    Assisted Solution

    Glary Utilities will remove empty folders on your computer and it is free.  Hope that works.
    LVL 21

    Assisted Solution

    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

    LVL 43

    Accepted Solution

    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.

    LVL 51

    Assisted Solution

    by:Bill Prew

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
    This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
    This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    26 Experts available now in Live!

    Get 1:1 Help Now