0

I currently have a method that lists all sub-directories and I think I need to supplement it for another method.

private static List<string> GetDirectories(string directory, string searchPattern)
{
    try
    {
        return Directory.GetDirectories(directory, searchPattern).ToList();
    }
    catch (UnauthorizedAccessException)
    {
        return new List<string>();
    }
}

I then call it like this:

var directories = GetDirectories(directory, fileExtension);

I can list all sub-directories on the next level but not the level inside of it. The catch is my code won't exit if there's a folder I don't have access to.

e.g. when I pass "C:\" and "*.*" I can get

C:\Folder1  

C:\Folder2 

C:\Folder3

but not the folders inside of it.

I am trying to make a List that would make it so that if I pass C:\\ and \*.xls, I'll be able to get the result below as a List:

Directory     | File Count

C:\Folder1    |      3   (3 files under \Folder with and xls extension)

C:\Folder\Sub |   2

C:\Folder2    | 5

and so on.

Thank you in advance.

Neuron
  • 5,141
  • 5
  • 38
  • 59
mPatrick
  • 15
  • 1
  • 6
  • It doesn't look like you're making a recursive call, which is why it's not returning nested results. But I'm not really sure what your question is here. – BurnsBA Oct 17 '17 at 14:54
  • See also https://stackoverflow.com/questions/3710617/list-recursively-all-files-and-folders-under-the-given-path?rq=1 – BurnsBA Oct 17 '17 at 14:55
  • I can recommend the following recursive method: https://stackoverflow.com/questions/14305581/method-to-get-all-files-within-folder-and-subfolders-that-will-return-a-list – programmierkind Oct 17 '17 at 14:58
  • Thank you for the direction! I'm looking into these threads right now! – mPatrick Oct 17 '17 at 15:01

1 Answers1

0

You need a recursive function to run your search query against each child directory found.

void GetChildDirectories(string path, string pattern, Dictionary<string, int> stats)
{
    try
    {
        var children = Directory.GetDirectories(path);
        var count = Directory.GetFiles(path, pattern).Length;

        stats[path] = count;

        foreach (var child in children)
            GetChildDirectories(child, pattern, stats);
    }
    catch (UnauthorizedAccessException e)
    {
        stats[path] = -1;
        return;
    }
}

Once this function returns, you can print results like this:

    var stats = new Dictionary<string, int>();
    string path = "C:\\", pattern = "*.txt";

    GetChildDirectories(path, pattern, stats);

    Console.WriteLine("Directory | Count");

    foreach (var key in stats.Keys)
    {
        if (stats[key] == -1)
            Console.WriteLine("Unable to access path: {0}", key);
        else
            Console.WriteLine("{0} | {1}", key, stats[key]);
    }

The recursion is likely to blow up in your face, badly, as written in example. These days there are thousands of directories in %systemdrive%. A more stable solution would be stack-based non-recursive iteration.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32