Solved

Find/Return All Files in a directory tree - VB6

Posted on 2009-07-15
3
315 Views
Last Modified: 2012-06-22
I would likw a very simple piece of code that (given a starting folder) will loop and return into an array every directory/filename combination (format ready for Open statement) in a directory and all its subdirectories. No special order is required. Thanks.
0
Comment
Question by:centech-group
  • 2
3 Comments
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24864105
Try this
 
 

Option Explicit
 
Dim mlngIndex As Long
Dim gstrFiles() As String
 
Private Sub Command1_Click()
    Call ListFiles("c:\temp")
End Sub
 
Private Sub ListFiles(strFolder As String)
    Dim oFs As Object
    Dim oFile As File
    Dim oFolder As Folder
    Dim oSubFolder As Folder
   
    Set oFs = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFs.GetFolder(strFolder)
    If Err.Number > 0 Then Exit Sub
   
    For Each oFile In oFolder.Files
        mlngIndex = mlngIndex + 1
        ReDim Preserve gstrFiles(mlngIndex)
        gstrFiles(mlngIndex - 1) = oFolder.Path & "\" & oFile.Name
    Next
    
    For Each oSubFolder In oFolder.SubFolders
        Call ListFiles(oSubFolder.Path)
    Next
  
    'Clean up
    Set oFs = Nothing
    Set oFolder = Nothing
 
End Sub

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 24864590
ReDim Preserve will slow it down significantly if you have a large number of files...

...you could use a Collection instead like in my old example here:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_21205213.html
0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24895764
Idle_Mind is correct about the ReDim slowing things down. But your question did say "loop and return into an array every directory/filename combination". Probably should have given you what you needed not what you asked for.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSRS expression Issue finding a string 10 76
using Access 8 68
passing parameter in sql procedure 9 61
Advice in Xamarin 21 78
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now