VBA Excel递归文件夹搜索停止

时间:2018-10-22 13:10:28

标签: excel vba search directory fso

我有脚本来扫描文件夹,以查找文件名包含特定文本的文件。该脚本可以工作,但是它会在不完成整个文件夹的扫描的情况下停止运行(我达到了16663次扫描,有限制吗?)我不知道为什么脚本会停止。任何帮助是极大的赞赏。

我最初使用的是此帖子Get list of sub-directories in VBA

中发布的代码

更新:我正在扫描的驱动器是网络驱动器。我现在的假设是,由于连接中的停顿,脚本停止了。目前,我正在尝试不同的方法来解决此问题...

Sub LoopThroughFilePaths()

    Application.StatusBar = True
    Application.ScreenUpdating = False

    Counter = 1

    Dim strPath As String
    strPath = "V:\50"                            ' folder to scan

    Dim myArr
    myArr = GetSubFolders(strPath)

    Application.StatusBar = False
    Application.ScreenUpdating = True

End Sub

使用的功能GetSubFolders

Function GetSubFolders(RootPath As String)

    Application.ScreenUpdating = False

    Dim fso As Object
    Dim fld As Object
    Dim sf As Object
    Dim myArr
    Dim output As String
    Dim StrFileOut As String
    VAR_01_output = "D:\output"                  'Location to copy found files to

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.GetFolder(RootPath)

    Dim StrFile As String
    StrFile = Dir(fld + "\*labsuite*")           'wild card search for files

    Do While Len(StrFile) > 0
        StrFileOut = Format(Now(), "hh-mm-ss") & "_" & StrFile ' rename files
        FileCopy fld + "\" + StrFile, VAR_01_output + "\" + StrFileOut 'copy found files to output folder
        StrFile = Dir
    Loop

    For Each sf In fld.SubFolders
        ReDim Preserve Arr(Counter)
        Arr(Counter) = sf.Path
        Counter = Counter + 1

        On Error Resume Next
        myArr = GetSubFolders(sf.Path)
        On Error Resume Next

        'ActiveWorkbook.Sheets(1).Cells(1, 1).Value = Counter
        Application.StatusBar = sf.Path

        DoEvents
    Next

    GetSubFolders = Arr

    Set sf = Nothing
    Set fld = Nothing
    Set fso = Nothing

End Function

0 个答案:

没有答案