Home » excel » Excel VBA – Using multiple instances of Dir()?

Excel VBA – Using multiple instances of Dir()?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am using excel to migrate a large file structure into a new folder and re-order many of the folders. I am using the Dir() function to cycle through every folder, and also cycle through files… but I am running into an issue where the second Dir() function overwrites the first. Is there a way to setup two instances of Dir()?

Sub GetFolders()
    Dim oldFolderPath As String
    Dim folder As String
    Dim copyFolderDir As String
    Dim newFolderDir As String
    Dim strFile As String

    oldFolderPath = "C:\Users\jordanharris\Desktop\PATIENT FILES\A\"

    newFolderDir = "C:\Users\jordanharris\Desktop\PATIENT FILES\A v2\"

    'The goal here is to loop through every file in a folder (without knowing how many or their names)
    folder = Dir(oldFolderPath, vbDirectory) 'First Dir()

    Do While folder <> ""
       If (GetAttr(oldFolderPath & folder) And vbDirectory) = vbDirectory Then

           MkDir newFolderDir & folder & "\APPS-AWARDS\"

           copyFolderDir = oldFolderPath & folder & "\DWSS-EA\"

           'The goal here is to copy every file in the folder 'DWSS-EA' to the new folder 'APPS-AWARDS'
           strFile = Dir(copyFolderDir & "*.*") ' This Dir is overwriting the Dir above
           Do While Len(strFile) > 0 
              Name copyFolderDir & strFile As newFolderDir & folder & "\APPS-AWARDS\" & strFile
              'Get next file using Dir
              strFile = Dir()
           Loop
       End If

        'Get Next Folder using Dir 
        folder = Dir() 'Error on this line because Dir is being overwritten
    Loop

End Sub

As you can see, I am using two instances of Dir, which is leading to this error where I cannot go to the next folder. I originally thought I would just put the second instance of Dir in its own Sub, like so…

Sub AppsAwards (newFolderDir As String, oldFolderPath As String, folder As String)
    MkDir newFolderDir & folder & "\BENEFITS\APPS-AWARDS\"

    copyFolderDir = oldFolderPath & folder & "\DWSS-EA\"

    strFile = Dir(copyFolderDir & "*.*")

    Do While Len(strFile) > 0
        Name copyFolderDir & strFile As newFolderDir & folder & "\BENEFITS\APPS-AWARDS\" & strFile
        strFile = Dir()
    Loop
End Sub

… and call this in place of the original code …

...
AppsAwards newFolderDir, oldFolderPath, folder
...

But it acts exactly the same, the calling of Dir within the sub overwrites the original Dir.

Is there a way to have two instances of Dir()? And if not, is there a workaround for this?

Edit (Solution):

Thanks to Noodles for a good workaround. This is how I implemented it in my code…

Sub ProcessFolder(FolderPath As String, newFolderPath As String)
    On Error Resume Next
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")

    Set fldr = fso.GetFolder(FolderPath)
    Set fls = fldr.Files

    For Each Thing In fls
        Name FolderPath & Thing.Name As newFolderPath & Thing.Name
    Next

End Sub    

And then I placed this line in my original code…

...
ProcessFolder oldFolderPath & folder & "\DWSS-EA\", newFolderDir & folder & "\BENEFITS\APPS-AWARDS\"
...
How to&Answers:

You use recursion to walk a tree. This is VBScript so pastable into VBA. PS The help says Visual Basic allows you to process drives, folders, and files in two different ways: through traditional methods such as the Open statement, Write#, and so forth, and through a new set of tools, the File System Object (FSO) object model.

'On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Dirname = InputBox("Enter Dir name")
'Searchterm = Inputbox("Enter search term")
ProcessFolder DirName

Sub ProcessFolder(FolderPath)
    On Error Resume Next
    Set fldr = fso.GetFolder(FolderPath)

    Set Fls = fldr.files

    For Each thing in Fls
         msgbox Thing.Name & " " & Thing.path 
    Next


    Set fldrs = fldr.subfolders
    For Each thing in fldrs
        ProcessFolder thing.path
    Next

End Sub