【问题标题】:Using VBA to Loop Through Files in Sub-folders使用 VBA 循环遍历子文件夹中的文件
【发布时间】:2020-10-09 09:51:58
【问题描述】:

我正在尝试编写一个宏来遍历文件夹中所有子文件夹中的所有文件,然后调用另一个宏来解压缩文件。解压缩代码工作正常,但循环代码不工作。

E:\Downloads\data\ADVANCED\2020\Feb\1\file 路径有 5 个子文件夹,每个子文件夹包含一个要解压缩的文件。

谁能看出问题所在?

谢谢

这是循环代码:

Sub LoopAllFilesInAFolder()

Dim fileName As Variant
fileName = Dir("E:\Downloads\data\ADVANCED\2020\Feb\1\")

While fileName <> ""

    Call UnZipFile

    fileName = Dir
Wend

End Sub

【问题讨论】:

  • 这能回答你的问题吗? Loop through files in a folder using VBA?
  • 您的 UnZipFile 子/函数如何知道要解压缩哪些文件夹文件?它不应该像参数一样接受文件夹路径吗?我要求这样做是为了发布在所有子文件夹之间迭代的答案并为找到的文件夹做“某事”。如果文件夹路径未传递给被调用函数,您的代码似乎在解压缩方面没有做任何事情......

标签: excel vba


【解决方案1】:

请尝试使用下一种方法:

Sub LoopAllFilesInAFolderAndSubfolders()
 Dim FSO As Object, foldName As String

 'Create FSO object
 Set FSO = CreateObject("Scripting.FileSystemObject")
 'Set the folder name to a variable
 foldName = "E:\Downloads\data\ADVANCED\2020\Feb\1\"
 'Call the recursive itSubFolders Sub, which makes the job:
 itSubFolders FSO.GetFolder(foldName)
End Sub

以及递归子提取子文件夹中的所有文件:

Sub itSubFolders(FSOFolder As Object)
 Dim objSubfold As Object, objFile As Object

 'Iterate between al subfolders
 Sub itSubFolders(FSOFolder As Object)
 Dim objSubfold As Object, objFile As Object

 'Iterate between al subfolders
 For Each objSubfold In FSOFolder.SubFolders
    itSubFoldersAndFiles objSubfold
 Next

 'iterate for files
 For Each objFile In FSOFolder.Files
    'Debug.Print  objFile.path
    itSubFolders objFile 'I think, the code should send the file path as parameter, to the existing Function/Sub
 Next
End Sub

您可以测试上述递归 Sub,取消注释 'Debug.Print "Subfolder: " &amp; objFile.path 行并注释以下行。它将在即时窗口中返回所有文件路径。

注意:上述递归 Sub 也适用于子文件夹内的所有子文件夹...

【讨论】:

  • @rob:还活着吗?你收到了两个答案。你有时间测试一下吗?
【解决方案2】:

文件夹子文件夹中的文件 (FileSystemObject)

  • 首先使用Debug.Print 进行测试,看看结果列表是否包含您需要解压缩的确切文件。
  • 要消除任何混淆,请考虑以下路径:
    C:\Test1\Test2
    C:\Test3\Test4
    • 如果FolderPath = "C:",则代码将在文件夹Test1Test3 中查找文件。
    • 它将不会C:Test2Test4 中找到文件。

守则

Option Explicit

Sub LoopAllFilesInAFolder()
    
    Const FolderPath As String = "E:\Downloads\data\ADVANCED\2020\Feb\1\"
    Const Extension As String = "zip"
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim fldr As Object
    Set fldr = fso.GetFolder(FolderPath)
    Dim subFldr As Object
    Dim fil As Object
    For Each subFldr In fldr.SubFolders
        For Each fil In subFldr.Files
            If StrComp(fso.GetExtensionName(fil.Path), Extension, _
                       vbTextCompare) = 0 Then
                Debug.Print fil.Path
                'UnZipFile fil.path
            End If
        Next fil
    Next subFldr
    
End Sub

' If you want to learn more about the File System Object, add a reference
' to it via 'VBE>Tool>Microsoft Scripting Runtime'.
' Now its intellisense will be enabled and you should use the following code:

Sub LoopAllFilesInAFolderLearn()
    
    Const FolderPath As String = "E:\Downloads\data\ADVANCED\2020\Feb\1\"
    Const Extension As String = "zip"
    Dim fso As New FileSystemObject
    Dim fldr As Folder
    Set fldr = fso.GetFolder(FolderPath)
    Dim subFldr As Folder
    Dim fil As File
    For Each subFldr In fldr.SubFolders
        For Each fil In subFldr.Files
            If StrComp(fso.GetExtensionName(fil.Path), Extension, _
                       vbTextCompare) = 0 Then
                Debug.Print fil.Path
                'UnZipFile fil.Path
            End If
        Next fil
    Next subFldr

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 2017-04-06
    • 2021-10-16
    • 2016-02-12
    • 1970-01-01
    相关资源
    最近更新 更多