簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM