使用Excel VBA列出某些模式的文件

如何在用户指定的目录中列出所有与特定模式相匹配的文件? 这应该在所选目录的子文件夹内recursion地工作。 我也需要一个方便的方法(如树控件)列出它们。

看来,几个回答谈论recursion,一个关于正则expression式。 以下是将这两个主题放在一起的一些代码。 我从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 

作为一个通用指针,请看Application.FileSearch,recursion函数,Userforms和“Microsoft TreeView控件”。

FileSearch可以用来查找匹配模式的文件夹中的文件,recursion函数可以调用自己,直到所有的path都被耗尽,UserForm可以托pipe控件来显示数据,TreeView控件可以显示文件系统。

请记住,有预构build的function/控件可用于显示文件系统,例如Application.GetOpenFileName,Application.GetSaveAsFileName,Microsoft WebBrowser(给定'file:// …'URL)。

尝试Windows脚本 – 文件系统对象。 这个可以从vba创build的COM对象具有列出目录等的function。

您可以在MSDN上find文档

不完全是你所要求的,但我想我会在这里发布,因为它是相关的。

这是从http://www.cpearson.com/excel/FOLDERTREEVIEW.ASPXfind的代码进行修改

这需要参考Microsoft脚本运行时

 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 

我看到上面的人已经回答了如何通过文件树进行recursion,这可能会让您感兴趣的是在文件/文件名中search模式。 VBA的一个function是允许使用正则expression式。

 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 

您可以使用它来search文件名称的模式。 我build议使用正则expression式来获取更多关于如何使用正则expression式的信息

Interesting Posts