vba 如何获取目录中带有 ESY 扩展名的所有文件的列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3017318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:32:04  来源:igfitidea点击:

How to get list of all files with ESY extension in a directory?

vbasearchfilesystemsexcel-2007file-extension

提问by l--''''''---------''''''''''''

In VBA, how do I get a list of all the files with a specific extension in a specific directory?

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

i am unable to do Application.FileSearch, because i am using excel 2007

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

采纳答案by HansUp

In response to your comment "so how many times do i know to run it?", this example runs until it lists all the files whose names match strPattern. Change the strFolderconstant.

回应您的评论“我知道运行多少次?” ,此示例将一直运行,直到它列出名称与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

回答by mohnston

Dir("C:\yourPath\*.ESY", vbNormal) Returns the first file with esy extension. Each subsequent call to Dir() returns the next.

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

回答by Mike Woodhouse

Alternative option: use the "Microsoft Scripting Runtime" library (check it in Tools...References) for the FileSystemObject family of objects. Something like the following, perhaps:

替代选项:对 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

回答by Rocky Scott

The following code runs about 19 times faster than using FileSystemObject. On my machine, finding 4000 files in three different drectories took 1.57 seconds using FileSystemObject, but only 0.08 seconds using this code.

以下代码的运行速度比使用 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

Sample usage:

示例用法:

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

(The "DoEvents" is not necessary, but allows you to use Pause/Break if needed.)

(“DoEvents”不是必需的,但允许您在需要时使用暂停/中断。)