Avatar of shivermetimbers
shivermetimbers asked on

Need help iterating directories

Hello,

I need to write a program that will sweep through a directory (e.g. C:\Program Files\My Software) and any sub-directories and return an array of all the files it finds.

If anyone could point me in the right direction it would be much appreciated.

Many thanks,
Shivermetimbers.
Visual Basic Classic

Avatar of undefined
Last Comment
zzzzzooc

8/22/2022 - Mon
dds110

ASKER CERTIFIED SOLUTION
Mike Tomlinson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
dreffed

Hi Shivermetimbers...

YOu can use a recursice function and use the FileSystemObject...
In VB6.0
This code will use the filesystem object, and recursively scan the folders, I wrote it to add the files to a treeview

 in the declaration section
private m_files() as string

' in another function
Sub main()
    Dim m_fso As New FileSystemObject
    Dim m_folder As Folder

    Set m_folder = m_fso.getFolder(Me.txtPath.Text)
    scanFiles(m_folder)
End Sub

Private Sub scanFiles(thisFolder As Folder, optional extension As String = "")
    ' recursive function to scan the tree below the current path and
    ' load the files into the tree
    ' if extension is specified then filter
    dim i as long      
    Dim m_file As File
    Dim m_addFile As Boolean
   
    For Each m_file In thisFolder.Files
        ' add this file to the tree view
        ' simple compare, it extension mathces the file extension set the flag,
        ' if not extension is given then add the file.
       If Len(extension) > 0 Then
            If StrComp(Right(m_file.Name, Len(extension)), extension, vbTextCompare) <> 0 Then
                ' don't process this
                m_addFile = False
            Else
                m_addFile = True
            End If
        Else
            m_addFile = True
        End If
       
        ' can we add this file to the array
        If m_addFile Then
            ' get the array size and increment.
            on error resume next
            i = ubound(m_files)
            if err.number <> 0 then
                i = 0
                err.clear
            End If
            i = 1 + 1
           
            ' reinstate the error handler
            on error goto 0
           
            ' load the file details
            redim preserve m_files(i)
            m_files(i) = m_file.Name
        End If
    Next
   
    Dim m_folder As Folder
    For Each m_folder In thisFolder.SubFolders
        scanFiles m_folder, extension
    Next
   
End Sub

I hope this helps

Dreffed
zzzzzooc

The below works for me. Returned the same # of files in the "system32" folder as it does when going to "properties". It's recursive and I just pass the files around as chunks of strings (thought of trying arrays but I'd have to combine them then). As for the reason of "pagefile.sys", it errors when checking it's attribute so I had to ignore it. :] You can modify the attributes to search for also (I decided to have it return system files/etc).

Form1:
---------------

Option Explicit
Private Sub Form_Load()
    Dim sArray() As String, lLoop As Long
    sArray = Split(DirSearchAll("C:\windows\system32\"), vbCrLf)
    For lLoop = LBound(sArray) To UBound(sArray)
        'Print files 1 by 1 to debug window
        Debug.Print sArray(lLoop)
    Next lLoop
    MsgBox UBound(sArray) & " files found!"
End Sub
Private Function DirSearch(ByVal sSearchDir As String, bDir As Boolean) As String
    Dim sDir As String, sFind As String
    sDir = Dir(sSearchDir, vbDirectory + vbHidden + vbNormal + vbSystem + vbReadOnly)
    Do Until sDir = vbNullString
        If sDir <> "." And sDir <> ".." Then
            If bDir = True And sDir <> "pagefile.sys" Then
                If GetAttr(sSearchDir & sDir) And vbDirectory Then
                    DirSearch = DirSearch & sSearchDir & sDir & vbCrLf
                End If
            ElseIf bDir = False And sDir <> "pagefile.sys" Then
                If Not GetAttr(sSearchDir & sDir) And vbDirectory Then
                    DirSearch = DirSearch & sSearchDir & sDir & vbCrLf
                End If
            End If
        End If
        sDir = Dir()
        DoEvents
    Loop
End Function
Private Function DirSearchAll(ByVal sSearchDir As String) As String
    Dim sFiles As String, sDirs As String
    Dim sDirsSplit() As String, lLoop As Integer
    sFiles = DirSearch(sSearchDir, False)
    sDirs = DirSearch(sSearchDir, True)
    sDirsSplit = Split(sDirs, vbCrLf)
    For lLoop = LBound(sDirsSplit) To UBound(sDirsSplit) - 1
        sFiles = sFiles & DirSearchAll(sDirsSplit(lLoop) & "\")
    Next lLoop
    DirSearchAll = sFiles
End Function

Your help has saved me hundreds of hours of internet surfing.
fblack61