David Gard
David Gard

Reputation: 12087

How do you append on array to another?

I'm writing a function to check for multiple file extensions in a folder, and then return the matching file names, but I am having a problem returning the results.

The function 'works', but obviously every time it loops, it reallocates the results of Directory.GetFiles() to Files, as opposed to appending them. Can anyone please tell me how to do this?

And in case anyone wishes to know, I'm doing this because, as far as I can tell, there is no built in way of searcing for multiple patterns with Directory.GetFiles(). If this is wrong, please correct me. Thanks.

Private Function GetFiles(Path As String, SearchPattern As String)

    Dim SearchPatterns() As String = SearchPattern.Split("|") ' The patterns to search
    Dim Files() As String = Nothing ' The files to return to the user

    For Each Pattern In SearchPatterns

        Files = Directory.GetFiles(Path, Pattern)
        Array.Sort(Files)

    Next

    Return Files

End Function

Upvotes: 0

Views: 200

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125660

Use List(Of T) class and List(Of T).AddRange method instead:

Private Function GetFiles(Path As String, SearchPattern As String)

    Dim SearchPatterns() As String = SearchPattern.Split("|") ' The patterns to search
    Dim Files As New List(Of String)

    For Each Pattern In SearchPatterns
        Files.AddRange(Directory.GetFiles(Path, Pattern))
    Next

    Return Files.OrderBy((Function(f) f)).ToArray()
End Function

Upvotes: 2

Related Questions