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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Eddie Hunter
  • 33
  • 1
  • 1
  • 4

5 Answers5

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);
    }
  }
}
Jason
  • 86,222
  • 15
  • 131
  • 146
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
}
juergen d
  • 201,996
  • 37
  • 293
  • 362
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);
    }
Community
  • 1
  • 1
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.

Justin Self
  • 6,137
  • 3
  • 33
  • 48
-1

this will delete all empty (sub)folders in a given directory

https://stackoverflow.com/a/16688997/2408998

Community
  • 1
  • 1
Jamil
  • 1
  • 1