user429400
user429400

Reputation: 3335

Read strings to array of unknown size

I know that this question was asked before, but I can't find the answer I'm looking for. I want to iterate through a directory and read all the files into an array. I don't know how many files the directory contains, therefore I don't know the size of the array. What is the best way for me to achieve this? I read "ReDim" is a performance killer, but I don't find any list-like collection available...

Am I missing something?

Upvotes: 0

Views: 3317

Answers (2)

user2140173
user2140173

Reputation:

Either

Redim arr(0) as String

then

arr(ubound(arr)) = "newFilename"
ReDim Preserve arr(ubound(arr)+1)

then after the loop

ReDim Preserve arr(ubound(arr)-1)

to shrink it by the last empty element.


As alternative to using an array. You can use Collection or Dictionary.

Collections do not need to know their size at initialization. You add items using their Add() method.

To see a real example of implementation see this and that


If you do decide to go with the Dir() function and iterate to see how many files there are here's a sample function

Sub Main()

    ReDim arr(FilesInDirectoryCount("C:\...")) As String

End Sub

Function FilesInDirectoryCount(path As String)
  Dim f As String, c As Long
  f = Dir$(path)
  Do While Len(f) <> 0
    c = c + 1
    f = Dir$
  Loop
  FilesInDirectoryCount = c
End Function

or use this

Sub CountFiles()
    Dim strDir As String
    Dim fso As Object
    Dim objFiles As Object
    Dim obj As Object
    Dim lngFileCount As Long

    strDir = "C:\path..."

    Set fso = CreateObject("Scripting.FileSystemObject")

    Set objFiles = fso.GetFolder(strDir).Files

    lngFileCount = objFiles.count

    MsgBox lngFileCount

    Set objFiles = Nothing
    Set fso = Nothing
    Set obj = Nothing

End Sub

Upvotes: 3

Bathsheba
Bathsheba

Reputation: 234865

Method 1

One way (and standard insofar that it crops up a lot) is to dimension the array somewhat arbitrarily but based on the average use case.

When you're about to exceed the array size, redimension doubling the number of elements.

Then at the end, resize it down to the actual required size.

Method 2 (Bunk)

Traverse the directory to get the size. Then run your code with the array sized correctly. Not feasible for file systems though due to the lack of atomicity. The approach can work well though in other use cases; don't use it here though.

Upvotes: 2

Related Questions