|
-
Jan 14th, 2005, 02:45 PM
#1
Thread Starter
Fanatic Member
GetFiles(searchPattern) question
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?
-
Jan 14th, 2005, 03:48 PM
#2
Re: GetFiles(searchPattern) question
no I think you would have to call a getfiles with each filter you wanted...
-
Jan 14th, 2005, 03:57 PM
#3
Thread Starter
Fanatic Member
Re: GetFiles(searchPattern) question [resolved]
-
Jan 14th, 2005, 04:15 PM
#4
Re: GetFiles(searchPattern) question
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...
-
Jan 14th, 2005, 04:42 PM
#5
Re: GetFiles(searchPattern) question
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.
-
Jan 14th, 2005, 05:00 PM
#6
Re: GetFiles(searchPattern) question
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
-
Jan 14th, 2005, 06:07 PM
#7
Re: GetFiles(searchPattern) question
here's a little function i knocked together ...
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
hope it helps.
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Jan 27th, 2009, 10:47 PM
#8
New Member
Re: GetFiles(searchPattern) question
I recently wrote a blog about this subject with a code sample:
http://deepelement.com/2009/01/24/sy...ion-selection/
-
Jan 27th, 2009, 10:59 PM
#9
Re: GetFiles(searchPattern) question
While we're on the subject, here's some extension methods for the DirectoryInfo class in .NET 3.5+:
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
You can't do the same for the Directory class because Directory.GetFiles is a Shared method.
-
Jan 28th, 2009, 06:30 AM
#10
Re: GetFiles(searchPattern) question
-
Jan 28th, 2009, 06:36 AM
#11
Re: GetFiles(searchPattern) question
Nothing like bringing back a 4 year old thread eh
-
Jan 28th, 2009, 06:47 AM
#12
Re: GetFiles(searchPattern) question
 Originally Posted by chris128
Nothing like bringing back a 4 year old thread eh 
New solutions to old problems, or at least new variations on the old solutions.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|