【问题标题】:How to get list of all files with ESY extension in a directory?如何获取目录中所有扩展名为 ESY 的文件的列表?
【发布时间】:2011-03-02 07:58:50
【问题描述】:

在 VBA 中,如何获取特定目录中具有特定扩展名的所有文件的列表?

我无法做到Application.FileSearch,因为我使用的是 excel 2007

【问题讨论】:

    标签: vba search filesystems excel-2007 file-extension


    【解决方案1】:

    响应您的评论“那么我知道运行它多少次?”,此示例将一直运行,直到列出所有名称与 strPattern 匹配的文件。更改 strFolder 常量。

    Public Sub ListESY()
    Const strFolder As String = "C:\SomeFolder\"
    Const strPattern As String = "*.ESY"
    Dim strFile As String
    strFile = Dir(strFolder & strPattern, vbNormal)
    Do While Len(strFile) > 0
        Debug.Print strFile '<- view this in Immediate window; Ctrl+g will take you there
        strFile = Dir
    Loop
    End Sub
    

    【讨论】:

    【解决方案2】:

    Dir("C:\yourPath\*.ESY", vbNormal) 返回第一个带有 esy 扩展名的文件。 对 Dir() 的每个后续调用都会返回下一个。

    【讨论】:

    • 在 WHILE 或 DO 循环中测试结果的长度。当长度为 0 时,你就完成了。
    【解决方案3】:

    替代选项:对 FileSystemObject 系列对象使用“Microsoft Scripting Runtime”库(在工具...参考中检查它)。可能类似于以下内容:

    Public Function ESYFileCount(dir_path as String) as Long
    
    Dim fil As File
    
        With New FileSystemObject
            With .GetFolder(dir_path)
                For Each fil In .Files
                    If LCase(Right(fil.Name, 4)) = ".esy" Then
                        ESYFileCount = ESYFileCount + 1
                    End If
                Next
            End With        
        End With
    
    End Function
    

    【讨论】:

    • 如果此代码使用后期绑定而不是要求对 FSO 的引用,它会更好(并从我这里获得 +1)。
    • @David-W-Fenton - 我不明白为什么后期绑定会更好,需要说明一下吗?
    • 延迟绑定更好,因为域策略可以阻止 FSO 的自动化。对于不属于默认 Access 引用集的任何组件(很少有例外),后期绑定总是更好。通过缓存对它的引用并使用它而不是每次使用它时都重新初始化它,可以轻松避免性能损失。
    • 来自 Microsoft:尽可能使用早期绑定对象,因为它们允许编译器进行重要的优化。早期绑定对象比后期绑定对象要快得多,并且通过准确说明正在使用的对象类型使代码更易于阅读和维护。早期绑定的另一个优点是它启用了有用的功能,例如自动代码完成和动态帮助。早期绑定减少了运行时错误的数量和严重性,因为它允许编译器在编译程序时报告错误。
    • 在这方面比较晚,但后期绑定在应用程序中也有优势,这些应用程序将分布在具有不同标准和版本的系统中。
    【解决方案4】:

    以下代码的运行速度比使用 FileSystemObject 快大约 19 倍。在我的机器上,使用 FileSystemObject 在三个不同的目录中查找 4000 个文件需要 1.57 秒,但使用此代码只需 0.08 秒。

       Public Function CountFilesWithGivenExtension( _
              i_strFolderWithTerminalBackslant As String, _
              i_strExtensionIncludingPeriod As String _
              ) As Long
    
           If Len(Dir$(i_strFolderWithTerminalBackslant & "*" _
                 & i_strExtensionIncludingPeriod)) > 0 Then
    
              CountFilesWithGivenExtension = 1
    
              While Len(Dir$) > 0
    
                 CountFilesWithGivenExtension = _
                       CountFilesWithGivenExtension + 1
    
                 DoEvents
    
              Wend
           Else
              CountFilesWithGivenExtension = 0
           End If
    
       End Function
    

    示例用法:

       Debug.Print CountFilesWithGivenExtension("C:\", ".ex*")
    

    (“DoEvents”不是必需的,但允许您在需要时使用 Pause/Break。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-03
      • 2010-12-03
      • 2022-08-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2021-09-18
      • 2020-12-15
      相关资源
      最近更新 更多