Apply existing VBS folder search to sub folders?

前端 未结 2 1537
一个人的身影
一个人的身影 2021-01-28 02:38

I am using the following code to search a folder for a file name, open the file run an excel macro, save the file, and close. I would like to extend this to loop through sub fol

2条回答
  •  无人共我
    2021-01-28 02:50

    Well, apparently I'm not helpful...

    Dim path: path = "C:\Users\ntunstall\Desktop\test"
    Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
    'Call this to trigger the recursion.
    Call TraverseFolders(fso.GetFolder(path))
    
    Sub TraverseFolders(fldr)
      Dim f, sf
      ' do stuff with the files in fldr here, or ...
      For Each f In fldr.Files
        If InStr(f.Name, "OPS") > 0 Then
          Call RunMacroAndSaveAs(f, "Main")
        ElseIf InStr(f.Name, "Event") > 0 Then
          Call RunMacroAndSaveAs(f, "Events")
        End If
      Next
      For Each sf In fldr.SubFolders
        Call TraverseFolders(sf)  '<- recurse here
      Next
    
      ' ... do stuff with the files in fldr here.
    End Sub
    

    Taken from the method by @ansgar-wiechers - A: Recursively access subfolder files inside a folder which I already flagged as a duplicate.

    Have tested this using

    WScript.Echo f.Name
    

    in place of the RunMacroAndSaveAs() Sub Procedure if it is still erroring the issue lies there as this recursion works fine.

提交回复
热议问题