Web Analytics

Recently, I asked iTunes to organize my music directory (getting music from different sources other than just the iTunes store left it a little messy and I decided I’d let iTunes do its thing) and it did a great job; however, it left a bunch of empty directories. Although these empty directories didn’t pose any performance impact, they just looked… messy and I decided I’d delete them. I started doing this one by one and soon realized I had over 50 empty directories and sub directories. Being lazy, I decided to run a command to remove these directories. I’ve done this a lot in Linux so I figured it was easy; well, not quite, but it’s also not that hard. In this guide, I’ll show you what I did and hope you can find this useful in some way.

Remove Empty Directories Using the Command Line

Note: “Directory” and “folder” interchangeable terms in this context.

What I decided to do was list out all directories and parse them out to a batch file. To do this:

1. Open the command prompt (press Winkey+R, type cmd, and press Enter.)

2. Change the directory to the base directory containing the empty folders and sub-folders. In my case I switched to the Y: drive with the command:


and then changed to my iTunes music directory with the command

cd Y:\Music\iTunes\iTunes Media\Music

3. Run the following command:

dir /ad/b/s | sort /r >>out.bat

This command does the following:

dir – Runs the directory command

/ad – Selects files with the “directory” attribute i.e. all folders

/b – Base listing i.e. just folder names

/s – Include sub-folders

| – Pipe output from the dir /ad/b/s command to the following command (sort.)

sort – Sort the output

/r –  In reverse order. So, when we get to the next step we’ll be able to delete empty subfolders before we delete the parent folders (otherwise the parent folders wouldn’t be seen as empty.) When making this guide, I missed this parameter and couldn’t work out why it was only deleting some folders — before realizing there was a pattern to the results.

>>out.bat – Created an output of the command in out.bat in the same directory you’re working in (in this case, the iTunes music folder.)

4. Now open out.bat in a good text editor that can handle regular expressions (RegEx) i.e. Notepad++. We’re going to modify this output to turn it into a series of commands that with remove the directories (RD) one by one without deleting directories with files in them (default for the RD command.)

5. Press CTRL+R to open the Replace tool. Replace the first part of the directory path (i.e. Y:\Music\iTunes) with RD “[Directory path] RD in front of it. This basically adds RD (remove directory) to the front of every line:


Find: Y:\Music\iTunes\iTunes

Replace: RD “Y:\Music\iTunes\iTunes

Note: Do this in either Normal or Extended search mode.

6. Now to add a double quote to the end of each line. You can either do this manually (which I am too lazy to do) or by using a regular expression (you could also do it in word using paragraph marks as your search criteria.)

You’ll want to find each line and replace it with the line and a double quote i.e.

Find: ^(.*)$

Replace: \1″

Click Replace All to update the whole file.

7. Now add the word PAUSE to the end of the batch file to show the output in case there are any errors:

8. The next two steps are optional but I advise you do them to ensure your directories are empty.

As Windows creates thumbnail files by default, directories with media files in them will likely have a thumbs.db file in each of them. First, you’ll need to show hidden files and then search the base directory for thumbs.db and delete them all (even in the folders that still contains files that you don’t want to delete.)

9. As iTunes also adds a bunch of .jpg files to each directory (that it didn’t clear up when rearranging them), you’ll need to delete these too. Search for *.jpg and delete them all (this will delete the files from directories you want to keep but iTunes will replace them if needed.)

10. Now the fun part: double click your saved batch file to run it. Click Run when prompted.

11. As we added the PAUSE at the end of the file, the command prompt will show us the output. Notice a lot of directories were not deleted as they are not empty. That’s a good thing.

12. Now go to the directory and check that the empty folders are deleted.

This may take some time the first time you do it, but I am sure it will save you a ton of time in the future.

About Rich

Rich is the owner and creator of Windows Guides; he spends his time breaking things on his PC so he can write how-to guides to fix them.

Free PC tips by email

Search Windows Guides


10 thoughts on “Delete Empty Directories and Subdirectories [How To]”

  1. Uvais says:

    Good tip Rich :-)

    1. Ismailssk says:

      Thanks a lot for your post.. It helped me a lot…..For this program I am googling from past one week
      Thanks So much

  2. Steve says:

    This is a lot of work and it yields a result that can only be used once in one directory. A cmd script containing a couple of FOR loops could be used for a universal tool to remove empty directories. I’ve whipped up one and it is pasted below. I can explain it but I don’t want to waste the time with documentation unless someone cares…

    @echo off
    echo **************************** DeleteEmpty.cmd *******************************
    echo * *
    echo * This utility deletes empty directories below the current directory. *
    echo * *
    echo * Just navigate to the “highest” directory below which you want to remove *
    echo * any empty directories and execute this utility. *
    echo * *
    echo * You will be asked to verify the directory and if you would like to *
    echo * perform a test of the possible results. If you choose the test option, *
    echo * the directories that would be deleted are just listed. *
    echo * *
    echo ****************************************************************************
    echo. & echo.
    echo Current Directory: %CD%
    choice /c ytn /N /M “Is this the correct directory to start from? [(Y)es, (N)o or (T)est]: ”
    set ANSWER=%errorlevel% & if %ANSWER% equ 003 goto :EOF
    set LABEL=DELETE & if %ANSWER% equ 002 set LABEL=TEST
    echo. & echo.

    ::Enumerate directories below current and pass directory name to testing loop then call “action”
    for /D %%d in (*) do for /F “usebackq delims=,” %%a in (`”if not exist “%%d*.jpg” echo “%%d””`) do call :%LABEL% %%d
    goto :END

    echo Directory deleted: %* && rd /s /q %*
    exit /b

    echo Directory that would have been deleted: %*
    exit /b

    echo Operation Completed

  3. Webwalker says:

    find . -empty

    Oh, wait…Windows. Nevermind. :)

  4. Webwalker says:

    find . -empty

    Oh, wait…Windows. Nevermind. :)

  5. Anon says:

    I just concatenate in Excel, it’s just as easy. Good post tho.

  6. warlock666 says:

    the will delete all empty folders in current folder and all subfolders using windows 7 (tested on 32bit)open notepadcopy and paste this line below for /f “delims=” %%d in (‘dir /s /b /ad ^| sort /r’) do rd “%%d”save file rmdir.bat (change filetype in notepad to all files)move this file to the root folder you wish to delete empty folders and double clickthats all there is to it simple

    1. L3 says:

      This works perfectly and it’s so much simpler!

  7. FrustratedAnon says:

    Didn’t even work, eeeuuuurrrggghhhh.

  8. Jobes says:

    OH Wow, thank you so much! This cut down on all the empty directories hanging about on my Droid Razr. One thing I can’t stand that Droid doesn’t, and that’s have an easy way of cleaning up all the empty directories it leaves behind. So much tidier yay!

Comments are closed.

Computer tips in your inbox
Sign up for the Windows Guides newsletter to get PC tips and access to free Windows books (More details)

Subscribe now
Popular Guides

See which sites have been visited on your PC (even if private browsing mode is used)

Create a Windows 7 System Repair Disc

Best Free Anti-malware

Hibernate vs. Sleep vs. Shut-Down

i3, i5, and i7; Dual, Quad, Hexa Core Processors. How to they Differ?

Intel's Ivy Bridge Processor: new Features

Windows Guides on Facebook