首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取一个目录中所有扩展名为ESY的文件的列表?

如何获取一个目录中所有扩展名为ESY的文件的列表?
EN

Stack Overflow用户
提问于 2010-06-11 02:28:46
回答 4查看 35.6K关注 0票数 15

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

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

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-06-11 03:04:20

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

代码语言:javascript
复制
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
票数 15
EN

Stack Overflow用户

发布于 2010-06-11 02:54:56

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

票数 3
EN

Stack Overflow用户

发布于 2010-06-11 04:06:03

另一种选择:对FileSystemObject对象家族使用"Microsoft Scripting Runtime“库(在Tools...References中检查它)。可能类似于以下内容:

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3017318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档