locked
Delete all empty folders on Server? RRS feed

  • Question

  • im fairly new to C# and wasnt sure were i should start on this and if anyone had any code or anything that could help? im basically looking for a way to make a button scan my home server and delete all the empty folders?

    james griffith

    Tuesday, July 24, 2012 2:26 AM

Answers

  • May be you are not aware of static key word. Please go thru this page to know more about static keyword and how to use static members.

    Anyways, put the method inside a class like below,

    public class Utility
    {
       static private void RemoveEmptyFolders(string folderPath)
       {
          var allFolders = Directory.GetDirectories(folderPath);
          if (allFolders.Length > 0)
          {
            foreach (string folder in allFolders)
            {
                RemoveEmptyFolders(folder);
            }
          }
    
          if (Directory.GetDirectories(folderPath).Length == 0 && Directory.GetFiles(folderPath).Length == 0)
          {
            Directory.Delete(folderPath);
          }
       }
    }

    Then you can call this method directly as Utility.RemoveEmptyFolders. For example, if i want to clean all empty folders under C:\, i can do this as,

    Utility.RemoveEmptyFolders("C:\\");

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by Lisa Zhu Friday, July 27, 2012 8:18 AM
    Thursday, July 26, 2012 5:41 AM

All replies

  • Would you please check-out below thread

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/2239b45a-3f19-4a2a-b5cd-050b3ee32b9d


    Regards,
    Ahmed Ibrahim
    SQL Server Setup Team
    My Blog
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you.
    This can be beneficial to other community members reading the thread.

    Tuesday, July 24, 2012 2:39 AM
  • Hi,

    server usually means a Database. So there are no folders on it.

    But you can check tables on the server and delete them if empty.

    Is this what you want?


    Mitja

    Tuesday, July 24, 2012 2:51 AM
  • no i actually have a server at my house that all the computers on my network can save there things to. its like 9 terrabites ish. id like to scan the files and delete all the folders that are empty

    james griffith

    Tuesday, July 24, 2012 4:35 AM
  • Use the below function I have created for you. Just pass the Server directory or folder path and it will delete all empty folders. But remember that you must have credentials to delete folders on server.

    static private void RemoveEmptyFolders(string folderPath)
    {
        var allFolders = Directory.GetDirectories(folderPath);
        if (allFolders.Length > 0)
        {
            foreach (string folder in allFolders)
            {
                RemoveEmptyFolders(folder);
            }
        }
    
        if (Directory.GetDirectories(folderPath).Length == 0 && Directory.GetFiles(folderPath).Length == 0)
        {
            Directory.Delete(folderPath);
        }
    }

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, July 24, 2012 5:48 AM
  • thanks for the help adavesh but i couldnt get this to work properly, atleast the above code. most of it seemed to be working except the static private void was not working. somthing about needing a class i think, or atleast i thought that could solve the issue but it didint. im also using system.io;

    james griffith

    Wednesday, July 25, 2012 9:14 PM
  • May be you are not aware of static key word. Please go thru this page to know more about static keyword and how to use static members.

    Anyways, put the method inside a class like below,

    public class Utility
    {
       static private void RemoveEmptyFolders(string folderPath)
       {
          var allFolders = Directory.GetDirectories(folderPath);
          if (allFolders.Length > 0)
          {
            foreach (string folder in allFolders)
            {
                RemoveEmptyFolders(folder);
            }
          }
    
          if (Directory.GetDirectories(folderPath).Length == 0 && Directory.GetFiles(folderPath).Length == 0)
          {
            Directory.Delete(folderPath);
          }
       }
    }

    Then you can call this method directly as Utility.RemoveEmptyFolders. For example, if i want to clean all empty folders under C:\, i can do this as,

    Utility.RemoveEmptyFolders("C:\\");

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by Lisa Zhu Friday, July 27, 2012 8:18 AM
    Thursday, July 26, 2012 5:41 AM
  • Adavesh, oner q.

    I never understood, even if I didnt really get into this issue, why Calling same method name inisde a loop?

    What the benefit of this kind of code?

    I mean this:

    static private void RemoveEmptyFolders(string folderPath)
    {
        //...
        foreach (string folder in allFolders)
        {
             RemoveEmptyFolders(folder); //caling same method
        }
    }


    Mitja

    Thursday, July 26, 2012 8:02 AM
  • Yes Mitja. I am calling the same method recursively but passing different value.

    This is to walk through evey child folder of every folder in a folder :) Without using recursion it's pretty difficult to traverse to child folders especially when nested folders are very deep like below folder hierarchy

                        


    Please mark this post as answer if it solved your problem. Happy Programming!


    • Edited by Adavesh Thursday, July 26, 2012 8:14 AM
    Thursday, July 26, 2012 8:13 AM
  • Oh, damn I forgot about recusrion (I know what it is btw). And bg thx for an explanation. Now it makes sence.

    Thx ones again Adavesh, I really appreciate it.


    Mitja

    Thursday, July 26, 2012 12:52 PM