How to collect a list of file name from a directory

  • Thread starter Thread starter Richard Lewis Haggard
  • Start date Start date
R

Richard Lewis Haggard

Is there an easy way to construct a list of the names of all files whose
extension is, for example, .htm in a directory? I've managed to do by
searching through all files in the folder and detecting when 'htm' was the
extension, but I did so in a rather clumsy manner. Is there a more concise
way to accomplish the same thing?.

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim strPath As String
strPath = "C:\Documents and Settings\rhaggard\Application
Data\Microsoft\Signatures"

Dim folder
Set folder = fso.GetFolder(strPath)

Dim fileCollection
Set fileCollection = folder.Files

Dim file
Dim ext As String

Dim name As String
Dim arFiles() As String

Dim iItems As Integer
iItems = 0

For Each file In fileCollection
name = file.name

Rem Is this a ".htm" file?

If Right(name, 4) = ".htm" Then
name = Left(name, Len(name) - 4)
iItems = iItems + 1
ReDim Preserve arFiles(iItems)
arFiles(iItems) = name
End If
Next
 
Hi Richard,

I think it depends on what (do?) you need to do with the list.

For a simple list I would use an array to. If I need the ability of
deleting items or a simple way to ensure each item is unique than I
would use a collection. If I need my own properties or methods for the
items than I build classes.
For Each file In fileCollection
name = file.name

Rem Is this a ".htm" file?

If Right(name, 4) = ".htm" Then
name = Left(name, Len(name) - 4)
iItems = iItems + 1
ReDim Preserve arFiles(iItems)
arFiles(iItems) = name
End If
Next

I suppose fileCollection has a count property? Because the ReDim is very
expensive I would dim the array with the maximum of items first. Than
after the loop is finished I would do one "redim preserve" only to cut
all unneccessary elements at once.
 
Michael Bauer said:
Because the ReDim is very expensive I would dim the
array with the maximum of items first.
Than after the loop is finished I would do one "redim preserve" only
to cut all unneccessary elements at once.

Hi Michael,

I am always hesitant to intrude on someone else's thread, but I found
your statement above interesting.

Would you mind expanding on and, if possible, quantifying what you
mean by 'expensive'?

Thanks,

Alan.
 
Hi Alan,

you are welcome :-)
Would you mind expanding on and, if possible, quantifying what you
mean by 'expensive'?

I mean expensive in use of resources. A simple example for quatifying:

<sample>
Option Explicit

Private Declare Function GetTickCount Lib "kernel32" () As Long
Private lBeginTime As Long
Private lStopTime As Long
Const CNTS As Long = 999999

Sub test1()
Dim i&
Dim cnt&
Dim arr() As String

lBeginTime = GetTickCount

For i = 0 To CNTS
If i Mod 2 = 0 Then
ReDim Preserve arr(cnt)
arr(cnt) = "test"
cnt = cnt + 1
End If
Next

Debug.Print (GetTickCount - lBeginTime) / 1000
End Sub

Sub test2()
Dim i&
Dim cnt&
Dim arr() As String

lBeginTime = GetTickCount

ReDim arr(CNTS)

For i = 0 To CNTS
If i Mod 2 = 0 Then
arr(cnt) = "test"
cnt = cnt + 1
End If
Next

ReDim Preserve arr(cnt - 1)

Debug.Print (GetTickCount - lBeginTime) / 1000
End Sub
</sample>

On my machine (1600+, 512RAM) test1 takes about 5.5 seconds, test2 about
0.5 seconds.

A list with 5 elememts doesn´t matter but, I think, it cannot be harmed
to know about this.
 
Michael Bauer said:
I mean expensive in use of resources. A simple example for
quatifying:

<sample>

{Snipped sample code}
</sample>

On my machine (1600+, 512RAM) test1 takes about 5.5 seconds, test2
about
0.5 seconds.

A list with 5 elememts doesn´t matter but, I think, it cannot be
harmed to know about this.

Thanks Michael.

I am doing something quite like that, and it does take a noticeable
amount of time using a ReDim at each step (constructing an array of
around 50 unique elements built from a non-unique list of about 3000)
so I will change it to your suggestion.

Regards,

Alan.
 
Back
Top