What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleSearch for files that match multiple patterns in Visual Basic 6
DescriptionThis example shows how to search for files that match multiple patterns in Visual Basic 6.
Keywordsfiles, search, search for files, GetFiles, FindFiles, search patterns, Visual Basic 6, VB 6
CategoriesFiles and Directories

The Dir function lets you search a directory for files. The FindFiles method shown in the following code searches for files that match multiple patterns.

' Return a list of files that match the patterns.
Private Function FindFiles(ByVal dirname As String, ByVal _
    patterns As String) As Collection
Dim pattern_array() As String
Dim pattern As Variant
Dim files As Collection
Dim filename As String

    ' Separate the patterns.
    pattern_array = Split(patterns, ";")

    Set files = New Collection
    ' Loop through the files in the directory.
    filename = Dir$(dirname)
    Do While Len(filename) > 0
        ' See if the name matches any pattern.
        For Each pattern In pattern_array
            If LCase$(filename) Like LCase$(pattern) Then
                files.Add filename
                Exit For
            End If
        Next pattern

        filename = Dir$()

    Set FindFiles = files
End Function
The function splits its patterns parameter into an array of separate pattern strings. It then uses Dir to loop through the files in the target directory. For each file, it loops through the patterns to see if the file's name matches any of the patterns. If a file matches a pattern, the code adds it to the result collection.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.