在VBA中,如何获取特定目录中具有特定扩展名的所有文件的列表?
我无法使用Application.FileSearch,因为我使用的是excel 2007
发布于 2010-06-11 03:04:20
为了回应您的评论“那么我知道要运行它多少次?”,此示例将一直运行,直到它列出了名称与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发布于 2010-06-11 02:54:56
vbNormal(“C:\yourPath\*.ESY”,Dir)返回第一个扩展名为esy的文件。随后对Dir()的每次调用都会返回下一个。
发布于 2010-06-11 04:06:03
另一种选择:对FileSystemObject对象家族使用"Microsoft Scripting Runtime“库(在Tools...References中检查它)。可能类似于以下内容:
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 Functionhttps://stackoverflow.com/questions/3017318
复制相似问题