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:
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.
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.