Skip to content

.Net Create a String Array or an ArrayList of FileInfo by Searching Directory and all Subdirectories using Wildcards

January 3, 2012

    Const dir = "C:\TestDir"
    Const folderSearchPattern = "*"
    Const fileSearchPattern = "*.exe"

    '##########  Wildcard Search Patterns  ##########  
    '*  Zero or more characters.
    '?  Exactly zero or one character.

    'Characters other than the wildcard specifiers represent themselves. For example, the searchPattern string "*t" 
    'searches for all names in path ending with the letter "t". The searchPattern string "s*" searches for all 
    'names in path beginning with the letter "s".

    '######  Return a string array with the all of the filenames  ###### 
    ReadOnly ar1() As String = Directory.GetFiles(dir, fileSearchPattern, SearchOption.AllDirectories)

    '######  Return an arraylist of FileInfo for all of the files  ######
    ReadOnly ar2 As ArrayList = FileSearch(dir, folderSearchPattern, fileSearchPattern)
    Private Shared Function FileSearch(ByRef directory As String, Optional ByRef folderSearchPattern As String = "*", Optional ByRef fileSearchPattern As String = "*") As ArrayList
        FileSearch = New ArrayList
        Dim di As New DirectoryInfo(directory)
        For Each f As FileInfo In di.GetFiles(fileSearchPattern)
            FileSearch.Add(f)
        Next
        For Each sdi As DirectoryInfo In di.GetDirectories(folderSearchPattern, SearchOption.TopDirectoryOnly)
            FileSearch.AddRange(FileSearch(sdi.FullName, folderSearchPattern, fileSearchPattern))
        Next
    End Function
    
Advertisements

From → .Net

One Comment
  1. Anas Almansuri permalink

    Thank you soooooooooooooooo much JoCoder 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: