joseamirandavelez
New member
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:
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:
Any help would be greatly appreciated!
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.Type: System.String[]
String. Pattern to be matched. Required.
Any help would be greatly appreciated!