Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

Find/Return All Files in a directory tree - VB6

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
centech-group
Asked:
centech-group
  • 2
1 Solution
 
Bob HoffmanDeveloperCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
Bob HoffmanDeveloperCommented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now