We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Need help iterating directories

Medium Priority
186 Views
Last Modified: 2010-05-02
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.
Comment
Watch Question

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
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

Commented:
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

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.