[英]List files of certain pattern using Excel VBA
如何在用户指定的目录中列出与特定模式匹配的所有文件? 这应该在所选目录的子文件夹内递归工作。 我还需要一种方便的方式(例如树控件)来列出它们。
似乎有两个答案涉及到递归,一个关于正则表达式。 这是一些将两个主题放在一起的代码。 我从http://vba-tutorial.com获取了代码
Sub FindPatternMatchedFiles()
Dim objFSO As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objRegExp As Object
Set objRegExp = CreateObject("VBScript.RegExp")
objRegExp.pattern = ".*xlsx"
objRegExp.IgnoreCase = True
Dim colFiles As Collection
Set colFiles = New Collection
RecursiveFileSearch "C:\Path\To\Your\Directory", objRegExp, colFiles, objFSO
For Each f In colFiles
Debug.Print (f)
'Insert code here to do something with the matched files
Next
'Garbage Collection
Set objFSO = Nothing
Set objRegExp = Nothing
End Sub
Sub RecursiveFileSearch(ByVal targetFolder As String, ByRef objRegExp As Object, _
ByRef matchedFiles As Collection, ByRef objFSO As Object)
Dim objFolder As Object
Dim objFile As Object
Dim objSubFolders As Object
'Get the folder object associated with the target directory
Set objFolder = objFSO.GetFolder(targetFolder)
'Loop through the files current folder
For Each objFile In objFolder.files
If objRegExp.test(objFile) Then
matchedFiles.Add (objFile)
End If
Next
'Loop through the each of the sub folders recursively
Set objSubFolders = objFolder.Subfolders
For Each objSubfolder In objSubFolders
RecursiveFileSearch objSubfolder, objRegExp, matchedFiles, objFSO
Next
'Garbage Collection
Set objFolder = Nothing
Set objFile = Nothing
Set objSubFolders = Nothing
End Sub
并非完全符合您的要求,但我想我会将其发布在此处,因为它与此相关。
这是从位于http://www.cpearson.com/excel/FOLDERTREEVIEW.ASPX的代码中修改的
这需要参考Microsoft Scripting Runtime 。
Sub ListFilePaths()
Dim Path As String
Dim Files As Long
Path = "C:\Folder"
Files = GetFilePaths(Path, "A", 1)
MsgBox "Found " & Files - 1 & " Files"
End Sub
Function GetFilePaths(Path As String, Column As String, StartRow As Long) As Long
Dim Folder As Scripting.Folder
Dim SubFolder As Scripting.Folder
Dim File As Scripting.File
Dim FSO As Scripting.FileSystemObject
Dim CurrentRow As Long
Set FSO = New Scripting.FileSystemObject
Set Folder = FSO.GetFolder(folderpath:=Path)
CurrentRow = StartRow
For Each File In Folder.Files
Range(Column & CurrentRow).Value = File.Path
CurrentRow = CurrentRow + 1
Next File
For Each SubFolder In Folder.SubFolders
CurrentRow = GetFilePaths(SubFolder.Path, Column, CurrentRow)
Next SubFolder
GetFilePaths = CurrentRow
Set Folder = Nothing
Set FSO = Nothing
End Function
我看到我上面的人已经回答了如何遍历文件树,这可能使您感兴趣的是搜索文件/文件名中的模式。 这是VBA的功能,将允许使用正则表达式。
Private Function RegularExpression(SearchString As String, Pattern As String) As String
Dim RE As Object, REMatches As Object
'Create the regex object'
Set RE = CreateObject("vbscript.regexp")
With RE
.MultiLine = False
.Global = False
.IgnoreCase = True
'set the search pattern using parameter Pattern'
.Pattern = Pattern
End With
'Search for the pattern'
Set REMatches = RE.Execute(SearchString)
If REMatches.Count > 0 Then
'return the first match'
RegularExpression = REMatches(0)
Else
'nothing found, return empty string'
RegularExpression = ""
End If
End Function
作为一般指针,请看一下Application.FileSearch,递归函数,用户窗体和“ Microsoft TreeView控件”。
FileSearch可用于在与模式匹配的文件夹中查找文件,递归函数可以调用其自身,直到用尽所有路径为止,UserForm可以承载用于显示数据的控件,而TreeView控件可以显示文件系统。
请记住,有一些预构建的功能/控件可用于显示文件系统,例如Application.GetOpenFileName,Application.GetSaveAsFileName,Microsoft WebBrowser(具有“ file:// ...” URL)。
尝试使用Windows脚本-文件系统对象。 可以从vba创建的COM对象具有列出目录等的功能。
您可以在MSDN上找到文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.