Years ago I made a serious mistake when I installed, and briefly used, iTunes -- I gave it the ability to reorganize my music folders! Since then, all my nicely organized and sorted directories of (legally obtained) digital music have been disorganized and confused.
For example, I used to have a directory named "U2" where all my music for the group U2 was stored. However, iTunes decided that my organization structure was too simple, and broke it out into folders derived from the contributing artists. Now I have "Bono & The MDH Band", "Bono_Daniel Lanois", "Bono_Gavin Friday_Maurice Seezer", etc. It's a real mess. Additionally, it left the folder "U2" with a bunch of empty subdirectories! How rude!
I've made an effort, as of late, to try and clean-up this mess. In doing so, I've ended up with a lot of empty directories that previously contained music and/or subdirectories. After manually deleting a couple directories, I decided I needed to automate the process. I was surprised to find that this process is not nearly as straightforward as I thought it would.
In going about this process, I decided to do the following:
Let me break down how I accomplished these things.
To scan for empty directories, I created a method that starts with a predefined directory, and recursively iterates through all the subdirectories. During this iteration, I scan the folder to see if it contains any files using the GetFiles method. Rather than specify files with a specific file extension (like .mp3 or .wma) I decided to get all files by using the "*.*" pattern (directory happens to be DirectoryInfo object).
using System.IO; ... FileInfo[] files; files = directory.GetFiles("*.*");
However, many of the files are hidden (because WMP and iTunes creates album art that's hidden), so I decided to exclude hidden files. I did this by checking the hidden attribute of the file, and if it was turned on I ignored the file. If non-hidden files were found, I marked a local boolean value as true, indicating the folder contains files.
foreach (FileInfo file in files) { // only look for files that are not marked as hidden if (!((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)) { notEmpty = true; } }
Next, I want to check to see if the directory contains subdirectories, and if those subdirectories are empty. To do this I get all the subdirectories and call the same method that is currently executing. This provides a level of recursion that allows me to iteratively go through every subdirectory in a directory. Since the existence of a subdirectory means that the directory is not empty, I automatically mark the current directory as not empty.
// look through directories recursively DirectoryInfo[] dirs = directory.GetDirectories("*.*"); foreach (DirectoryInfo dir in dirs) { scanForEmptyDirectories(dir); notEmpty = true; }
Next, I add the directory path to a generic string collection so that I can later iterate through and delete the directories. The code here is very simple:
private static List<string> directoriesToDelete; directoriesToDelete = new List<string>(); ... if (!notEmpty) { // add folder to collection directoriesToDelete.Add(directory.FullName); }
This accomplished, I needed to write a method that logs empty directories, the deletion of directories, and errors. I created a method that uses the StreamWriter and appends text to a file. Very simple.
public static void logMessage(string logMessage) { using (StreamWriter writer = File.AppendText(logFile)) { writer.WriteLine(logMessage); writer.Flush(); } }
This method is called throughout the application, and writes any pertinent information to a log file (as you'll see below).
Now, to delete the directories, I iterate through my generic string collection. Since some of my directories are marked as read-only, I elected to change the attributes of all directories marked for deletion to normal. This removes the read-only flag, if it exists. Next, I performed a similar operation on the files left in the directory (e.g. hidden files) and removed the read-only flag. Once these two steps are complete, I can then delete the folder using the Delete method on the Directory object.
// iterate through the empty directories foreach (string directory in directoriesToDelete) { try { DirectoryInfo dir = new DirectoryInfo(directory); // set the attributes to normal; you cannot delete a readonly folder dir.Attributes = FileAttributes.Normal; string[] files = Directory.GetFiles(directory); // iterate through the files foreach (string file in files) { FileAttributes attributes = File.GetAttributes(file); if ((attributes & FileAttributes.ReadOnly) != 0) { // remove the readonly attribute from the files File.SetAttributes(file, ~FileAttributes.ReadOnly); } } // make sure to make the delete as recursive Directory.Delete(directory, true); // log the deletion logMessage(string.Format("Deleted: {0} ", directory)); } catch (Exception e) { // log the error logMessage(string.Format("Error deleting: {0} ", directory)); logMessage(string.Format("Error message: {0} ", e.Message)); logMessage("Exiting application"); // exit the console application Environment.Exit(0); } }
Notice that I catch errors, log them, and then exit the application. Also, the try/catch is embedded in the directory loop, so that the directory path can be included in the log (if it were the other way around, the directory path would be out of scope and unavailable).
Finally, given the above code, it is possible to make a parent directory empty by deleting its child directories. To resolve this, I wrapped the execution of my code in a while statement that only exists when a given condition returns false. This condition value returns false only if there hasn't been a single directory marked as empty. If any directory is marked as empty, the application performs an additional scan following the deletion of the directories it found empty. (Note: the runScan boolean value is a class-level variable that is set to true elsewhere in the code.)
while (runScan) { // assume that we won't have to run again runScan = false; // if the value returns true, then run it again // it would return true scanForEmptyDirectories(new DirectoryInfo(startingDirectory)); }
All in all, this was a very quick and efficient way to eliminate my empty music folders. Your needs may be slightly different, so make changes accordingly.
I've gone ahead and included a zip file with the full source code for this console appliation. Let me know if you have thoughts and/or suggestions!
EmptyFolders.zip (20.81 KB)
Remember Me
a@href@title, b
Page rendered at Tuesday, January 06, 2009 8:14:22 AM (Central Standard Time, UTC-06:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.