SearchFolders 集合

优质
小牛编辑
131浏览
2023-12-01

ScopeFolder 对象的集合,这些对象确定调用 FileSearch 对象的 Execute 方法时搜索的文件夹。

使用 SearchFolders 集合

使用 FileSearch 对象的 SearchFolders 属性返回 SearchFolders 集合,例如:

Set sfs = Application.FileSearch.SearchFolders

对于每个应用程序只有一个 SearchFolders 集合。集合的内容在调用它的代码执行结束后保持不变。因此,清除集合很重要,除非要在搜索中包括上一次搜索的文件夹。

可以使用 SearchFolders 集合的 Add 方法向 SearchFolders 集合中添加 ScopeFolder 对象,但是使用要添加的 ScopeFolder 对象的 AddToSearchFolders 方法更为简便,因为对于所有搜索只有一个 SearchFolders 集合。

SearchFolders 集合可视为对 FileSearch 对象的 LookIn 属性的补充。二者均指定搜索的文件夹并在执行搜索时使用。但是,如果只希望使用 LookIn 属性,应确保 SearchFolders 集合为空。相反,如果只希望使用 SearchFolders 集合,请在 Execute 方法之前将 LookIn 属性设置为 SearchFolders 集合第一个成员的路径。

本示例搜索本地计算机上每个名为 "1033" 的文件夹,查找所有 HTML 和 Microsoft Excel 文件。本示例使用 SearchFolders 集合、SearchScopes 集合和 ScopeFolders 集合。

本示例由两个例程组成。SearchEveryFolder 例程为要运行的例程。OutputPaths 例程独立于主例程,因为它将递归调用自身以浏览本地计算机的整个目录结构。

                        'Break out of the loop
                        lngCount = .FoundFiles.Count

                    End If
                Next lngCount
            End If
        End If
    End With
End Sub

'This subroutine loops through all of the ScopeFolders collections
'in a given ScopeFolders collection. It adds any folder
'that has the same name as the value of strFolder
'to the SearchFolders collection.
Sub OutputPaths(ByVal sfs As ScopeFolders, _
    ByRef strFolder As String)

    'Declare a variable as a ScopeFolder object
    Dim sf As ScopeFolder

    'Loop through each ScopeFolder object in the
    'ScopeFolders collection.
    For Each sf In sfs

        'Test to see if the folder name of the ScopeFolder
        'matches the value of strFolder. Use LCase to ensure
        'that case does not affect the match.
        If LCase(sf.Name) = LCase(strFolder) Then

            'Add the ScopeFolder to the SearchFolders collection.
            sf.AddToSearchFolders

        End If

        'Include a DoEvents call because there is the potential for this
        'loop to last a long time. The DoEvents call allows this process to
        'continue handling events.
        DoEvents

        'Test to see if the ScopeFolders collection in the
        'current ScopeFolder is empty. If it isn't empty, then
        'that means that the current ScopeFolder object contains subfolders.
        If sf.ScopeFolders.Count > 0 Then

            'This subroutine recursively calls itself so that
            'it can add the subfolders of the current ScopeFolder object
            'to the SearchFolders collection.
            Call OutputPaths(sf.ScopeFolders, strFolder)

        End If
    Next sf
End Sub

Sub SearchEveryFolder()

    'Declare variables that reference a
    'SearchScope and a ScopeFolder object.
    Dim ss As SearchScope
    Dim sf As ScopeFolder

    'Declare a variable to act as a generic counter.
    Dim lngCount As Long

    'Use a With...End With block to reference the
    'FileSearch object.
    With Application.FileSearch

        'Clear all the parameters of the previous searches.
        'This method doesn't clear the LookIn property or
        'the SearchFolders collection.
        .NewSearch

        'Specify the type of file for which to search.
        'Use the FileType property to specify the first type
        'and then add additional types to the FileTypes collection.
        .FileType = msoFileTypeWebPages
        .FileTypes.Add msoFileTypeExcelWorkbooks

        'Clear the SearchFolder collection by
        'looping through each ScopeFolder object
        'and removing it.
        For lngCount = 1 To .SearchFolders.Count
            .SearchFolders.Remove lngCount
        Next lngCount

        'Loop through the SearchScopes collection to find
        'the scope in which you want to search. In this
        'case the scope is the local machine.
        For Each ss In .SearchScopes
            Select Case ss.Type
                Case msoSearchInMyComputer

                    'Loop through each ScopeFolder in
                    'the ScopeFolders collection of the
                    'SearchScope object.
                    For Each sf In ss.ScopeFolder.ScopeFolders

                        'Call a function that loops through all
                        'of the subfolders of the root ScopeFolder.
                        'This function adds any folders named "1033" to the
                        'SearchFolders collection.
                        Call OutputPaths(sf.ScopeFolders, "1033")

                    Next sf
                Case Else
            End Select
        Next ss

        'Test to see if any ScopeFolders collections were added to
        'the SearchFolders collection.
        If .SearchFolders.Count > 0 Then

            'Set the LookIn property to the path of
            'the first ScopeFolder object in the SearchFolders
            'collection. This is here so that any previous
            'setting of the LookIn property doesn't affect
            'the search.
            .LookIn = .SearchFolders.Item(1).Path

            'Execute the search and test to see if any files
            'were found.
            If .Execute <> 0 Then

                'Display the number of files found.
                MsgBox "Files found: " & .FoundFiles.Count

                'Loop through the list of found files and
                'display the path of each one in a message box.
                For lngCount = 1 To .FoundFiles.Count
                    If MsgBox(.FoundFiles.Item(lngCount), vbOKCancel, _
                        "Found files") = vbCancel Then
FileSearch
SearchFolders
ScopeFolder