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

The GetDirectories method returns a read-only collection of strings representing the path names for the subdirectories in a directory. You can use the wildCards parameter to specify a specific pattern. If you would like to include the contents of subdirectories in the search, set the searchType parameter to SearchOption.SearchAllSubDirectories.

An empty collection is returned if no directories matching the specified pattern are found.

To find subdirectories with a specific pattern

Use the GetDirectories method, supplying the name and path of the directory you want to search. The following example returns all the directories in the directory structure that contain the word "Logs" in their name, and adds them to ListBox1.

For Each foundDirectory As String In
       My.Computer.FileSystem.GetDirectories(
           My.Computer.FileSystem.SpecialDirectories.MyDocuments,
           FileIO.SearchOption.SearchTopLevelOnly,
           "*Logs*")

    ListBox1.Items.Add(foundDirectory)
Next

Robust Programming

The following conditions may cause an exception:

See also