0 Replies - 1869 Views - Last Post: 24 June 2015 - 02:56 PM

#1 andrewsw   User is offline

  • Bouncy!
  • member icon

Reputation: 6563
  • View blog
  • Posts: 26,615
  • Joined: 12-December 12

Find subfolder(s)

Posted 24 June 2015 - 02:56 PM

To find a subfolder (or subfolders) when you are looking to match part of its name, people will often write a method, perhaps recursively, to search all folders and subfolders. The GetDirectories methods accept wildcard characters (* and ?) to pattern-match (sub)folders.

How to: Find Subdirectories with a Specific Pattern in Visual Basic

DirectoryInfo.GetDirectories Method (String, SearchOption)

We can apply LINQ extension methods to make the searches more specific.

Find the first subfolder that ends with "Testing":
    Dim found As String = My.Computer.FileSystem.GetDirectories("C:\Users\Andrew\Documents", _
                                          FileIO.SearchOption.SearchAllSubDirectories, _
                                          "*Testing").FirstOrDefault()
    If found IsNot Nothing Then
        MessageBox.Show(found)
    End If

(I could have used My.Computer.FileSystem.SpecialDirectories.MyDocuments.)

Get an array of all subfolders ending with "Testing":
    Dim found() As String = My.Computer.FileSystem.GetDirectories("C:\Users\Andrew\Documents", _
                                          FileIO.SearchOption.SearchAllSubDirectories, _
                                          "*Testing").ToArray()

Once you have this array you can perform further searching against it, rather than the more complex LINQ expressions that follow.

Get the first subfolder ending with "Testing", preceded by exactly four characters, "XXXXTesting":
    Dim found2 As String = My.Computer.FileSystem.GetDirectories("C:\Users\Andrew\Documents", _
                                  FileIO.SearchOption.SearchAllSubDirectories, _
                                  "*Testing").Where(Function(x) x.LastIndexOf("\"c) = x.LastIndexOf("Testing") - 5).FirstOrDefault()
    If found2 IsNot Nothing Then
        MessageBox.Show(found2)
    End If

It is actually easier to use the wildcard-expression "????Testing", where the question mark represents precisely one character, but this is our first example using LINQ.

Get the first subfolder ending with "Testing", preceded by exactly four digits; e.g. "1234Testing":
    Dim found3 As String = My.Computer.FileSystem.GetDirectories("C:\Users\Andrew\Documents", _
                            FileIO.SearchOption.SearchAllSubDirectories, _
                            "*Testing").Where(Function(x)
                                                  Return x.LastIndexOf("\"c) = x.LastIndexOf("Testing") - 5 _
                                                      AndAlso IsNumeric(x.Substring(x.LastIndexOf("\"c) + 1, 4))
                                              End Function).FirstOrDefault()
    If found3 IsNot Nothing Then
        MessageBox.Show(found3)
    End If

This post has been edited by andrewsw: 24 June 2015 - 03:13 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1