Multiple filetypes : For Each File As String in Directory.GetFiles

Xancholy

Well-known member
Joined
Aug 29, 2006
Messages
143
Programming Experience
Beginner
How can I use

For Each File As String in Directory.GetFiles

to return a list of files found of multiple filetypes ?

ie: *.gif, *.jpg, *.bmp

If this is not possible, please can I have a workaround?

Thanks for any help
 
Thanks. I need to be able to return multiple file types for me to process

For each file in...

do something

Next
 
As I understand it you can't use but one pattern at a time. I have tried several ways but none of them work. Here is what I use:
VB.NET:
        Dim DirInfo As New DirectoryInfo("d:\graphics")
        Dim files() As FileInfo = DirInfo.GetFiles("*.gif")
        For Each File As FileInfo In files
            TextBox1.Text &= File.Name.ToString & vbNewLine
        Next
        files = DirInfo.GetFiles("*.jpg")
        For Each File As FileInfo In files
            TextBox1.Text &= File.Name.ToString & vbNewLine
        Next
I used a textbox here but you can use a listbox or whatever you want.
 
Found it here

You can also use a Predicate in Findall

VB.NET:
    Public Sub LoadFiles()
        Dim dir As New DirectoryInfo("C:\")

        For Each File As FileInfo In Array.FindAll(dir.GetFiles(), AddressOf Predicate_DocFiles)
            Debug.Print(File.Name)
        Next
    End Sub

    Public Function Predicate_DocFiles(ByVal File As FileInfo) As Boolean
        Select Case File.Extension
            Case ".txt", ".xml", ".log"
                Return True
            Case Else
                Return False
        End Select
    End Function
 
Back
Top