【问题标题】:Recursive Search Through Subfolders BACK to Root Directory通过子文件夹递归搜索返回到根目录
【发布时间】:2019-05-11 20:00:08
【问题描述】:

我有一个函数可以搜索给定目录的子文件夹并找到我需要的文件名。但是,它只遍历一组子文件夹,找到第一个子文件夹,然后遍历子文件夹的末尾。但是,它然后就停止了。我浏览了各种线程并尝试了不同的选项,但没有任何乐趣。

我需要它然后循环回到根目录(例如,sPath=C:\Windows)并查看下一个子文件夹,遍历整个目录,返回根文件夹,依此类推,直到找到它需要的文件。我似乎无法让那部分工作,希望这里的人可以帮助指出我所缺少的。我试图将此设置保留在更高级别的根文件夹中,而不是必须从目录中的较低位置开始使其工作。这是函数:

Function recurse(sPath As String, strname As String, strName3 As String)

Dim FSO As New FileSystemObject
Dim myFolder As Scripting.Folder
Dim mySubFolder As Scripting.Folder
Dim myFile As Scripting.file    

Dim strJDFile As String
Dim strDir As String
Dim strJDName As String

Set myFolder = FSO.GetFolder(sPath)

' strName = Range("a2").Offset(0, 3)
strName3 = Replace(strName3, "/", " ")

For Each mySubFolder In myFolder.SubFolders
Debug.Print " mySubFolder: " & mySubFolder

For Each myFile In mySubFolder.Files        

    If "*" & myFile.Name & "*" Like "*" & strName3 & "*" Then
        strJDName = myFile.Name
        strDir = mySubFolder & "\"
        strJDFile = strDir & strJDName

        recurse = strJDFile

        Exit Function

    Else
        Debug.Print "  myFile.name: " & myFile.Name
    End If

Next

recurse = recurse(mySubFolder.Path, strname, strName3)

Next

End Function

【问题讨论】:

标签: excel vba recursion subdirectory


【解决方案1】:

如果您在 Windows 下运行 Excel,则可以根据自己的使用情况调整此例程。

  • 使用 Excel 文件夹选择器例程选择基本文件夹
  • 输入文件名掩码(例如:Book1.xls*
  • 使用Dir 命令窗口命令检查所有文件夹和子文件夹 中是否有以Book1.xls 开头的文件
  • 命令的结果被写入一个临时文件(在宏结束时被删除)
    • 有一种方法可以将其直接写入 VBA 变量,但这样做后我发现屏幕闪烁过多。
  • 然后将结果收集到 vba 数组中,并写入工作表,但您可以对结果进行任何操作。

Option Explicit
'set references to
'   Microsoft Scripting Runtime
'   Windows Script Host Object model
Sub FindFile()
    Dim WSH As WshShell, lErrCode As Long
    Dim FSO As FileSystemObject, TS As TextStream
    Dim sTemp As String
    Dim sBasePath As String
    Dim vFiles As Variant, vFullList() As String
    Dim I As Long
    Dim sFileName As String

    sTemp = Environ("Temp") & "\FileList.txt"

'Select base folder
With Application.FileDialog(msoFileDialogFolderPicker)
    .AllowMultiSelect = False
    If .Show = -1 Then 'if OK is pressed
        sBasePath = .SelectedItems(1)
    Else
        Exit Sub
    End If
End With

'File name mask
sFileName = InputBox("Entire File Mask", "File Finder")

Set WSH = New WshShell
lErrCode = WSH.Run("CMD /c dir """ & sBasePath & "\*" & sFileName & """ /A-D /B /S > " & sTemp, xlHidden, True)

If Not lErrCode = 0 Then
    MsgBox "Problem Reading Directory" & _
        vbLf & "Error Code " & lErrCode
    Exit Sub
End If


Set FSO = New FileSystemObject
Set TS = FSO.OpenTextFile(sTemp, ForReading, False, TristateFalse)

vFiles = Split(TS.ReadAll, vbLf)
TS.Close
FSO.DeleteFile sTemp
Set FSO = Nothing
Set WSH = Nothing

ReDim vFullList(1 To UBound(vFiles), 1 To 1)
For I = 1 To UBound(vFiles)
    vFullList(I, 1) = vFiles(I)
Next I

Dim rDest As Range
Set rDest = Cells(1, 2).Resize(UBound(vFullList, 1), UBound(vFullList, 2))

With rDest
    .EntireColumn.Clear
    .Value = vFullList
    .EntireColumn.AutoFit
End With

End Sub

【讨论】:

    猜你喜欢
    • 2014-07-07
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多