Avatar of wiggy353
wiggy353
 asked on

delete empty folders in a directory

I am trying to delete all of the empty directories in a directory. I have code that works:

        private static void processDirectory(string startLocation)
        {
            foreach (var directory in Directory.GetDirectories(startLocation))
            {
                processDirectory(directory);
                if (Directory.GetFiles(directory).Length == 0 && Directory.GetDirectories(directory).Length == 0)
                {
                    Directory.Delete(directory, false);
                    log.Rows.Add(DateTime.Now, "Success", "The directory named " + directory + " was deleted because it was empty.");
                }
            }
        }

Open in new window


which works good other than the line about adding the row to the log will not work because it is out of the context.

I tried putting this block of code into the main part of the code, but it will not work.

ideas?

Thanks.
.NET ProgrammingC#

Avatar of undefined
Last Comment
wiggy353

8/22/2022 - Mon
SAMIR BHOGAYTA

Hi, try this

using System;
using System.IO;

class Test
{
      
    public static void Main()
    {
        // Specify the directories you want to manipulate.
        string path = @"c:\MyDir";
        string subPath = @"c:\MyDir\temp";

        try
        {
            // Determine whether the directory exists.
            if (!Directory.Exists(path))
            {
                // Create the directory.
                Directory.CreateDirectory(path);
            }


            if (!Directory.Exists(subPath))
            {
                // Create the directory.
                Directory.CreateDirectory(subPath);
            }

            // This will succeed because subdirectories are being deleted.
            Console.WriteLine("I am about to attempt to delete {0}", path);
            if(Directory.ContentLength >0)
            {
            Directory.Delete(path, true);
            Console.WriteLine("The Delete operation was successful.");
            }

        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
        finally {}
    }
}
MajorBigDeal

If you are talking about the current context of a web page you could pass it into the static method by adding a parameter HttpContext currentContext and proving HttpContext.Current when you invoke the method.  Otherwise, you could pass a reference to the log when you call the static method - that would allow you to update it.
wiggy353

ASKER
samirbhogayta,

This will not work at all. It is not addressing any of the issues that I am working with. I do not know the names of the sub directories, and don't need to. I just need to delete them if they are empty. I do not want to write any lines of text to the console, I need to add rows to a table. I have read the text you posted on other web pages. It is not what I am looking for.

MajorBigDeal,

It is not dealing with a web page. It is not is the same context because the method that I posted is outside of the context of the main method which has the log table in it.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
fromer

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
wiggy353

ASKER
Perfect! I didn't realize I could pass a datatable as a parameter. Still learning. Thanks!