Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am looping through a set of directories using a File System Object, and I want to specify an additional directory to loop through. For example, I currently have:

Sub test()
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(Directory)
    Set colSubfolders = objFolder.Subfolders
    For Each objSubfolder In colSubfolders
        ' take some action
    End For
End Sub

But, I want to specify an additional folder to loop through, such as:

colSubfolders = colSubfolders + "additionalpath"
For Each objSubfolder In colSubfolders ....

Alternatively, is it possible to specify multiple objects in a loop command, such as:

For Each objSubfolder in colSubfolders, "additionalpath"
share|improve this question
You could just do 2nd separate loop. And if you want that second loop to be a list of folders, make an array of strings and iterate over that array. – Marc Apr 24 '12 at 17:50

2 Answers

up vote 3 down vote accepted

Simplest way is to break out the code that enumerates the directories and the code that "takes some action" and wrap it in a call that repeats what you need;

Sub foo()
    enumDirs "c:\temp\", "c:\music", "c:\as many as you like ..."
End Sub

Sub enumDirs(ParamArray strPaths() As Variant)
    Dim i As Long
    For i = 0 To UBound(strPaths)
        enumDir CStr(strPaths(i))
    Next
End Sub

Sub enumDir(strPath As String)
    Dim objFolder As Object, colSubfolders As Object, objSubfolder As Object
    Set objFolder = CreateObject("Scripting.FileSystemObject").GetFolder(strPath)
    Set colSubfolders = objFolder.Subfolders
    For Each objSubfolder In colSubfolders
        TakeSomeAction strPath, objSubfolder.Name
    Next
End Sub

sub TakeSomeAction(strRoot As String, strFoundPath As String)
    Debug.Print ">"; strRoot & ", " & strFoundPath
End sub
share|improve this answer
Thanks for the tip -- should have thought of wrapping up the action. – mike Apr 24 '12 at 18:07

Since you already have Directory set and are using it (looks like correctly, at that, but I'm not sure where you've set it or what you've set it to), why not just change Directory?

Using this link as reference, and building off your example:

*NB:This has been tested to work, but see my comment on this answer.

Sub test(Directory) ' I Changed this to allow for recursion... 
                    ' If Directory is set/used elsewhere, 
                    ' then use a different variable here, 
                    ' with Directory being passed only the first time (not below)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(Directory)
    Set colSubfolders = objFolder.Subfolders

    If colSubfolders.Count = 0 Then
        ' No more subfolders in this path
        ' Do your stuff here with Directory being the full path with all subdirectories
    Else
        ' This will skip if there no subfolders
        For Each objSubfolder In colSubfolders 
            test (Directory & objSubfolder.Name & "\") ' Recursion!
        Next
        ' Do more stuff here if you want to run also on non-terminating paths.
    End If        
End Sub
share|improve this answer
Looking at Alex's answer, then re-reading your question, I would say his method is better. Mine will dig into EVERY sub folder, and it looks like you just wanted 1/a few. – Gaffi Apr 24 '12 at 17:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.