GetFiles() with multiple wildcards

joseamirandavelez

New member
Joined
Apr 4, 2008
Messages
2
Location
Isabela, Puerto Rico
Programming Experience
3-5
Hi everyone. Im using the GetFiles method to return a list of files in a directory. Im searching the directory by specifying the extension to look for.

This is my code:
VB.NET:
Public Sub searchDirectory(ByVal subdir1 As String, ByVal recursive As Boolean)
        Try
            Dim file() As String
            Dim addfile As Boolean
            For Each ext In lstExtensions.Items

                file = Directory.GetFiles(subdir1, ext)
                For Each item In file
                    addfile = True
                    For Each exc In lstExceptions.Items
                        If "*" + Path.GetExtension(item) = exc Then
                            addfile = False
                        End If
                    Next
                    If addfile Then
                        filesfound.Add(item)
                    End If
                Next
            Next

            If recursive Then
                For Each subdir As String In System.IO.Directory.GetDirectories(subdir1)
                    searchDirectory(subdir, True)
                Next
            End If
        Catch ex As Exception
            lstErrors.Items.Add(ex.Message.ToString)
        End Try
    End Sub

Simple extensions like "*.doc" work. But I'm also searching for files with extensions of the form "*.do*" (I want to find files with extensions .do1, .do2,...). I dont want the program to find "*.doc" files so I added an exception list to clean the list using the exceptions.

The problem is when there is files called like this: "filename.document.txt" or "filename.doc.gz" for example. Searching for files with extensions "*.do*" and cleaning the files using the exception list still returns both files, since both strings match the pattern.

I guess this is OK since the parameters description in the documentation is:

wildcards
Type: System.String[]
String. Pattern to be matched. Required.​
My question then is, is there any code or method to return the files in a directory that match a given extension? Or maybe a function to compare if a string is like a given pattern?. That way I can split the filename, get the last extension and compare it to a string like "*.do*" to then decide if I need to add it or not.

Any help would be greatly appreciated!
 
VB.NET:
Dim filePaths As New List(Of String)

For Each filePath As String In IO.Directory.GetFiles("folder path here")
    If IO.Path.GetExtension(filePath).StartsWith(".do", StringComparison.CurrentCultureIgnoreCase) Then
        filePaths.Add(filePath)
    End If
Next filePath
You might choose to use a Regex instead of the StartsWith method.
 
That worked!

I ended up using the following code:

VB.NET:
    Public Sub searchDirectory(ByVal subdir1 As String, ByVal recursive As Boolean)
        Dim addfile As Boolean, item As String
        Try
            For Each ext In lstExtensions.Items
                For Each item In Directory.GetFiles(subdir1, ext)
                    If Path.GetExtension(item).StartsWith(StripChars(ext), StringComparison.CurrentCultureIgnoreCase) Then
                        addfile = True
                    End If
                    For Each exc In lstExceptions.Items
                        If ("*" + Path.GetExtension(item)).ToLower = exc.ToString.ToLower Then
                            addfile = False
                        End If
                    Next
                    If addfile Then
                        filesfound.Add(item)
                    End If
                Next
            Next
            If recursive Then
                For Each subdir As String In System.IO.Directory.GetDirectories(subdir1)
                    searchDirectory(subdir, True)
                Next
            End If
        Catch ex As Exception
            lstErrors.Items.Add(ex.Message.ToString)
        End Try
    End Sub

It is working without problems (so far). The regular expressions seems good but a little complicated given the time constraints I have. I may give it a try in a future version of the program.

Thanks for your help!
 
Back
Top