When you do GetFiles() you can specify a search pattern such as *.txt. How can I specify multiple patterns. For example, in windows search you do *.jpg;*.bmp;*.gif
I tried that but no luck and the msdn is no help. Is this possible?
Printable View
When you do GetFiles() you can specify a search pattern such as *.txt. How can I specify multiple patterns. For example, in windows search you do *.jpg;*.bmp;*.gif
I tried that but no luck and the msdn is no help. Is this possible?
no I think you would have to call a getfiles with each filter you wanted...
That stinks.. ok thanks.
if anyone else has any suggestions i would like to hear it as well... but i know using directoryinfo or directory classes getfiles method you can't do what you are looking for in a single query...
Suggestion...
Can you use a wildcard, retuning all file types and exclude the ones
as you pass thru the Directory?
strFile = GetFiles(*.*)
If Instr(strFile, ".jpg") Or Instr(strFile, "*.bmp) etc...
Bruce.
yeah I guess you could do that... but in that case though, there is no need for a filter at all, as *.* is technically NOT a filter :bigyello:
here's a little function i knocked together ...
hope it helps.VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim files() As String = GetPatternedFiles("C:\", New String() {"*.txt", "*.dll"}) For Each s As String In files Console.WriteLine(s) Next End Sub Private Function GetPatternedFiles(ByVal path As String, ByVal patterns() As String) As String() Dim ar As New ArrayList For Each pattern As String In patterns Dim temp() As String = Directory.GetFiles(path, pattern) If temp.Length > 0 Then ar.AddRange(temp) End If Next Dim ret(ar.Count) As String Array.Copy(ar.ToArray, ret, ar.Count) '/// copy the arraylist as an array object to a string array Return ret End Function
I recently wrote a blog about this subject with a code sample:
http://deepelement.com/2009/01/24/sy...ion-selection/
While we're on the subject, here's some extension methods for the DirectoryInfo class in .NET 3.5+:You can't do the same for the Directory class because Directory.GetFiles is a Shared method.vb.net Code:
Imports System.Runtime.CompilerServices Imports System.IO <Extension()> _ Public Module DirectoryInfoExtensions <Extension()> _ Public Function GetFiles(ByVal target As DirectoryInfo, _ ByVal ParamArray searchPatterns As String()) As FileInfo() Dim files As New List(Of FileInfo) For Each searchPattern As String In searchPatterns files.AddRange(target.GetFiles(searchPattern)) Next Return files.ToArray() End Function <Extension()> _ Public Function GetFiles(ByVal target As DirectoryInfo, _ ByVal searchPatterns As String(), _ ByVal searchOption As SearchOption) As FileInfo() Dim files As New List(Of FileInfo) For Each searchPattern As String In searchPatterns files.AddRange(target.GetFiles(searchPattern, searchOption)) Next Return files.ToArray() End Function End Module
Sweet.
Nothing like bringing back a 4 year old thread eh :D
New solutions to old problems, or at least new variations on the old solutions. :)Quote:
Originally Posted by chris128