9

Every example I see seems to be for recursively getting files in subdirectories uses files only. What I'm trying to do is search a folder for a particular subdirectory named "xxx" then save that path to a variable so I can use it for other things.

Is this possible without looping through all the directories and comparing by name?

Brad
  • 1,684
  • 4
  • 20
  • 36

6 Answers6

14

Well

Directory.GetDirectories(root);

will return you an array of the subdirectories.

You can then use Linq to find the one you're interested in:

IEnumerable<string> list = Directory.GetDirectories(root).Where(s => s.Equals("test"));

which isn't a loop in your code, but is still a loop nevertheless. So the ultimate answer is that "no you can't find a folder 'test' without looping".

You could add .SingleOrDefault() to the Linq, but that would depend on what you wanted to do if your "test" folder couldn't be found.

If you change the GetDirectories call to include the SearchOption SearchOption.AllDirectories then it will do the recursion for you as well. This version supports searching - you have to supply a search string - though in .NET Framework it's case sensitive searching. To return all sub directories you pass "*" as the search term.

Obviously in this case the call could return more than one item if there was more than one folder named "test" in your directory tree.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • 1
    An enhancement in .NET 4 is Directory.EnumerateDirectories which does not force you to load the entire list of directories into an array. It returns an IEnumerable directly. There is also Directory.EnumerateFiles for files. – Brian Walker Apr 01 '10 at 14:33
11
var foldersFound = Directory.GetDirectories(root, "test", SearchOption.AllDirectories)

This will return a string array with all the folders found with the given name. You can change the last parameter so that it only checks top level directories and you can change root to adjust where it is starting from.

Christopher B. Adkins
  • 3,499
  • 2
  • 26
  • 29
  • 1
    Is it possible to skip dirs that are forbidden? like if u search in C:\ and dont want to look in windows etc ? – Per G Mar 05 '19 at 11:40
1

First of all, "No, it is not possible without looping through all the directories and comparing by name".

I believe your real question is "Is there an existing API which will handle looping through all the directories and comparing by name for me?"

Yes, there is. It's called Directory.Exists():

var xxxPath = Path.Combine(parentFolder, "xxx");
if (Directory.Exists(xxxPath))
    savedPath = xxxPath;
James Curran
  • 101,701
  • 37
  • 181
  • 258
0

Yes, I believe that the only available solution (short of third party libraries) is a recursive search for the directory via name comparison.

jsight
  • 27,819
  • 25
  • 107
  • 140
0

You can use Windows Search which provides api for .Net too. Here is more detailed information: Windows Search 4.0 for Developers

Giorgi
  • 30,270
  • 13
  • 89
  • 125
0

Here is a snippet for searching for a folder using two filters while considering for the UnauthorizedAccessException, it can be refactored to use only one filter:

public static string FindGitPath(string firstFilter, string secondFilter, string initialPath)
    {
        string gitPath = string.Empty;
        foreach (var i in Directory.GetDirectories(initialPath)) {          
            try {
                foreach (var f in Directory.GetDirectories(i, firstFilter, SearchOption.AllDirectories)) {
                    foreach (var s in Directory.GetDirectories(f)) {
                        if (s == Path.Combine(f,secondFilter)) {
                            gitPath = f;
                            break;
                        }
                    }
                }       
            } catch (UnauthorizedAccessException) {
                Console.WriteLine("Path is not accessible: {0}", i);
            }                               
        }
        return gitPath; 
    }

Usage example:

Console.WriteLine("Retrieved the git database folder as {0}", FindGitPath("database",".git", "c:\\"));
Catalin
  • 541
  • 3
  • 7