3

I am writing a tool that will allow me to go though a fairly large list of Directories and Sub-directories. I would like it to delete a folder if there it is empty. I can delete folders and sub folders that are empty with this code:

string dir = textBox1.Text;
string[] folders = System.IO.Directory.GetDirectories(dir, "*.*", System.IO.SearchOption.AllDirectories);
foreach (var directory in folders)
{
    if (System.IO.Directory.GetFiles(directory).Length == 0 && System.IO.Directory.GetDirectories(directory).Length == 0)
    {
        System.IO.StreamWriter Dfile = new System.IO.StreamWriter(newpath, true);
        System.IO.Directory.Delete(directory);
    }
}

My question is how to have the code go though and check the folders after each delete because once it deletes a folder it could make the parent folder empty and should then should be deleted. Once the code does not find any folders or sub-folders that are empty it would exit.

4 Answers 4

7

Write a depth-first recursive function. As you complete each recursive call, check the current folder to see if it is empty. If it is, then delete it.

Something like this (pseudocode)

DeleteEmptyFolders(path)
{
  foreach Folder f in Path
  {
    DeleteEmptyFolders(f);

    if (f is empty)
    {
       Delete(f);
    }
  }
}
0
1

You can do this recursively like this (not tested):

void DeleteFolder(string folder) {
    string[] folders = System.IO.Directory.GetDirectories(folder, "*.*", System.IO.SearchOption.AllDirectories);
    foreach (var directory in folders)
    {
        DeleteFolder(directory);
    }

    //delete this folder if empty
}
0

Here's an idea (this isn't tested)

            while ( true )
            {
                DirectoryInfo parent = Directory.GetParent(current.FullName);
                if ( parent.GetFiles().Length == 0 && parent.GetDirectories().Length == 0 )
                {
                    current = parent;
                    current.Delete();
                }
                else
                {
                    break;
                }

            }

This walks up the tree of the current directory to delete any parent directories that are empty.

0

By the look of it your are trying to delete any folder which does not contain a file. I believe this would do the trick for you. I tested it with a small folder set that went 5 levels deep and a single file in a couple of locations. All folders which did not have files were deleted. All files were left intact. Small tweak to a snippet found... Thanks Matt Smith. Cannot delete directory with Directory.Delete(path, true)

string[] dirs = System.IO.Directory.GetDirectories(Directory.GetCurrentDirectory(), "*.*",SearchOption.AllDirectories);
            foreach (string d in dirs)
            {
                if (System.IO.Directory.Exists(d)) {
                    if (System.IO.Directory.GetFiles(d, "*.*", SearchOption.AllDirectories).Length == 0)
                    {
                        DeleteDirectory(d);
                    }
                }
            }

    public static void DeleteDirectory(string target_dir)
    {
        string[] dirs = Directory.GetDirectories(target_dir);

        foreach (string dir in dirs)
        {
            DeleteDirectory(dir);
        }

        Directory.Delete(target_dir, false);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.