Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Programming VBA / October 2004

Tip: Looking for answers? Try searching our database.

How to collect a list of file name from a directory

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Richard Lewis Haggard - 31 Oct 2004 15:37 GMT
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

----------
Richard Lewis Haggard
Michael Bauer - 31 Oct 2004 16:26 GMT
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
[quoted text clipped - 8 lines]
>         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.

Signature

Viele Grüße
Michael Bauer

> 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
[quoted text clipped - 39 lines]
> ----------
> Richard Lewis Haggard
Alan - 31 Oct 2004 21:30 GMT
> 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.
Michael Bauer - 31 Oct 2004 22:13 GMT
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.

Signature

Viele Grüße
Michael Bauer

> > Because the ReDim is very expensive I would dim the
> > array with the maximum of items first.
[quoted text clipped - 9 lines]
>
> Alan.
Alan - 31 Oct 2004 23:16 GMT
>> Would you mind expanding on and, if possible, quantifying what
>> you mean by 'expensive'?
[quoted text clipped - 3 lines]
>
> <sample>

{Snipped sample code}

> </sample>
>
[quoted text clipped - 4 lines]
> 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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.