The default selectfile's selectpattern only support on exention name.
For example, command below can only retrieve jpg image.
If you want both jpg and gif, you need two clause and combine the outputs.
System.IO.Directory.GetFiles("F:\", "*.jpg", IO.SearchOption.AllDirectories)
Alternatively, you can use LINQ and regular expression like below, but the code has issue when image has two extension like "Signature.jpg.pfs". Need a bit more works on the regular expression.
If someone knows how to write the regular expression, please let me know. Thanks
代码
Private Sub loadFiles()
For Each afile In SelectFilesWithExtenstions("F:\Learning\LoadDistinctFileInCheckListBox\file", ".jpg|.png|.gif")
Debug.Print(afile.name)
Next
End Sub
Private Function SelectFilesWithExtenstions(ByVal folder As String, ByVal extensions As String)
Dim dir As New System.IO.DirectoryInfo(folder)
Dim fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories)
Dim searchExtenstions As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex((extensions))
Dim queryGroupByExt = From afile In fileList _
Let matches = searchExtenstions.Matches(afile.FullName) _
Where (searchExtenstions.Matches(afile.FullName).Count > 0) _
Select Name = afile.FullName, _
Matches = From match As System.Text.RegularExpressions.Match In matches _
Select match.Value
Return queryGroupByExt
End Function
For Each afile In SelectFilesWithExtenstions("F:\Learning\LoadDistinctFileInCheckListBox\file", ".jpg|.png|.gif")
Debug.Print(afile.name)
Next
End Sub
Private Function SelectFilesWithExtenstions(ByVal folder As String, ByVal extensions As String)
Dim dir As New System.IO.DirectoryInfo(folder)
Dim fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories)
Dim searchExtenstions As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex((extensions))
Dim queryGroupByExt = From afile In fileList _
Let matches = searchExtenstions.Matches(afile.FullName) _
Where (searchExtenstions.Matches(afile.FullName).Count > 0) _
Select Name = afile.FullName, _
Matches = From match As System.Text.RegularExpressions.Match In matches _
Select match.Value
Return queryGroupByExt
End Function