Results 1 to 12 of 12

Thread: GetFiles(searchPattern) question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770

    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?

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: GetFiles(searchPattern) question

    no I think you would have to call a getfiles with each filter you wanted...

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2000
    Posts
    770

    Re: GetFiles(searchPattern) question [resolved]

    That stinks.. ok thanks.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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...

  5. #5
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    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.

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  7. #7
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: GetFiles(searchPattern) question

    here's a little function i knocked together ...
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim files() As String = GetPatternedFiles("C:\", New String() {"*.txt", "*.dll"})
    3.  
    4.         For Each s As String In files
    5.             Console.WriteLine(s)
    6.         Next
    7.  
    8.     End Sub
    9.  
    10.     Private Function GetPatternedFiles(ByVal path As String, ByVal patterns() As String) As String()
    11.         Dim ar As New ArrayList
    12.         For Each pattern As String In patterns
    13.             Dim temp() As String = Directory.GetFiles(path, pattern)
    14.             If temp.Length > 0 Then
    15.                 ar.AddRange(temp)
    16.             End If
    17.         Next
    18.         Dim ret(ar.Count) As String
    19.         Array.Copy(ar.ToArray, ret, ar.Count)  '/// copy the arraylist as an array object to a string array
    20.         Return ret
    21.     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]

  8. #8
    New Member
    Join Date
    Jan 2009
    Posts
    1

    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/

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Imports System.Runtime.CompilerServices
    2. Imports System.IO
    3.  
    4. <Extension()> _
    5. Public Module DirectoryInfoExtensions
    6.  
    7.     <Extension()> _
    8.     Public Function GetFiles(ByVal target As DirectoryInfo, _
    9.                              ByVal ParamArray searchPatterns As String()) As FileInfo()
    10.         Dim files As New List(Of FileInfo)
    11.  
    12.         For Each searchPattern As String In searchPatterns
    13.             files.AddRange(target.GetFiles(searchPattern))
    14.         Next
    15.  
    16.         Return files.ToArray()
    17.     End Function
    18.  
    19.     <Extension()> _
    20.     Public Function GetFiles(ByVal target As DirectoryInfo, _
    21.                              ByVal searchPatterns As String(), _
    22.                              ByVal searchOption As SearchOption) As FileInfo()
    23.         Dim files As New List(Of FileInfo)
    24.  
    25.         For Each searchPattern As String In searchPatterns
    26.             files.AddRange(target.GetFiles(searchPattern, searchOption))
    27.         Next
    28.  
    29.         Return files.ToArray()
    30.     End Function
    31.  
    32. End Module
    You can't do the same for the Directory class because Directory.GetFiles is a Shared method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: GetFiles(searchPattern) question

    Sweet.

  11. #11
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: GetFiles(searchPattern) question

    Nothing like bringing back a 4 year old thread eh
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: GetFiles(searchPattern) question

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width