How to retrieve all files from a given directory?
Posted on July 30, 2011
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 'File/Folder Handling - How to retrieve all files from a given directory? 'An array of filenames in a given directory and also demonstrates the correct way to 'set up the loop: Function GetFiles(filespec As String, Optional Attributes As VbFileAttribute) As String() Dim result() As String Dim filename As String, count As Long Const ALLOC_CHUNK = 50 ReDim result(0 To ALLOC_CHUNK) As String filename = Dir$(filespec, Attributes) Do While Len(filename) count = count + 1 If count > UBound(result) Then ' Resize the result array if necessary. ReDim Preserve result(0 To count + ALLOC_CHUNK) As String End If result(count) = filename ' Get ready for the next iteration. filename = Dir$ Loop ' Trim the result array. ReDim Preserve result(0 To count) As String GetFiles = result End Function |