Question Get files from subfolders to an array.

destrox

New member
Joined
Jun 16, 2010
Messages
4
Programming Experience
3-5
Here I have a function that imports files from a folder and puts them into an array. The problem is: the files from the subfolders aren't included. Can someone plz help me fixing this?


Public Function GetFilesByExtensions(ByVal strPath As String, ByVal colExtensions() As String) As IO.FileInfo()

Dim tmpCollection As New Collection

For Each strExtension As String In colExtensions
For Each aFile As IO.FileInfo In New IO.DirectoryInfo(strPath).GetFiles(strExtension)
tmpCollection.Add(aFile)
Next
Next


Dim tmpFiles(tmpCollection.Count - 1) As IO.FileInfo, i As Integer = 0

For Each aFile As IO.FileInfo In tmpCollection
tmpFiles(i) = aFile
i += 1
Next


Return tmpFiles




End Function
 
You can use this version of GetFiles with SearchOption.AllDirectories: DirectoryInfo.GetFiles Method (String, SearchOption) (System.IO)
Also, use a List(Of T) instead of Collection, in your case List(Of FileInfo). And you don't need to loop the results and add each file, you can use the AddRange method to add all results.
 
Back
Top