user3432981
user3432981

Reputation: 11

Find a Directory somewhere in Subdirectories

I want to find a directory somewhere among a lot of subdirectories using VB.NET. I have the path of the parent directory (D:\) and I have the name of the subdirectory (X) and I want to find this directory in any of the subdirectories of D:\. In D:\ I have 3 subdirectories (A, B, and C) and I want to find X (the name of the directory) inside A,BorC`. Is it possible to do that with VB.NET?

Upvotes: 1

Views: 111

Answers (2)

Steven Doggart
Steven Doggart

Reputation: 43743

You can do so using the Directory.EnumerateDirectories method, like this:

For Each i As String In Directory.EnumerateDirectories("D:\", "X", SearchOption.AllDirectories)
    Console.WriteLine("Matching Directory: " & i)
Next

Please note that there may be multiple matches. Also, be aware that if the directory tree is very large, it may take a long time for the method to find all of the matches. The key to that working for you is the SearchOption.AllDirectories option. By passing AllDirectories, that causes the method to search the entire directory tree below "D:\". If you omitted that parameter, or passed TopDirectoryOnly, it would only look at the directories that are directly children of "D:\". It would not search through all of the descendants.

If you just want to get an array of all the matching directories, you can alternatively use the Directory.GetDirectories method:

Dim matches() As String = Directory.GetDirectories("D:\", "X", SearchOption.AllDirectories)
If matches.Length > 0 Then
    Console.WriteLine("First match: " & matches(0))
End If

The advantage of the EnumerateDirectories method, though, is that, if you only care about the first match, you can exit the loop after processing the first match and skip searching the rest of the directory tree. From the MSDN article:

The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.

For instance, if you really only cared about the first match, it would be more efficient to do this:

For Each i As String In Directory.EnumerateDirectories("D:\", "X", SearchOption.AllDirectories)
    Console.WriteLine("First match: " & i)
    Exit For
Next

Or with LINQ:

Dim firstMatch As String = Directory.EnumerateDirectories("D:\", "X", SearchOption.AllDirectories).FirstOrDefault()
If firstMatch IsNot Nothing Then
    Console.WriteLine(firstMatch)
End If

Upvotes: 1

Chetan
Chetan

Reputation: 5085

Please check the below link http://msdn.microsoft.com/en-us/library/6ff71z1w%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

it will help you in getting all the directories in the specified path. Regarding the second argument that is the "pattern" try and check pattern as "" and hopefully should return and array of all directories and subdirectories

Then you can just compare/search for the file name in the returned array list using simple for loop and display the result which will show where the file is located

Upvotes: 0

Related Questions