如何解决从文件资源管理器搜索中提取文件名到Excel
这一直困扰着我一段时间,因为我觉得我的难题很少,但我无法将它们全部拼凑起来
因此,我的目标是能够在给定位置的所有.pdf文件中搜索文件内容(而不是文件名)中的关键字或短语,然后使用搜索结果填充excel电子表格。 / p>
在开始之前,我知道使用Acrobat Pro API可以很容易地做到这一点,但是我的公司不会为每个人支付许可证费用,因此这个宏可以正常工作。
Windows文件资源管理器搜索接受高级查询语法,并假设启用了正确的ifilter,将在文件内容内进行搜索。例如。如果您有一个名为doc1.docx的Word文档,并且文档中的文本为“ blahblahblah”,而您搜索“ blah”,则doc1.docx将作为结果出现。 据我所知,使用FileSystemObject不能实现这一点,但是如果有人可以确认这两种方法真的有用吗?
我有一个简单的代码,可以打开资源管理器窗口,并在给定位置的所有文件的内容中搜索字符串。搜索完成后,我将打开一个资源管理器窗口,其中列出了所需的所有文件。 如何获取此列表并使用这些文件的文件名填充excel?
dim eSearch As String
eSearch = "explorer " & Chr$(34) & "search-ms://query=System.Generic.String:" & [search term here] & "&crumb=location:" & [Directory Here] & Chr$(34)
Call Shell (eSearch)
解决方法
假设已为位置建立索引,则可以直接使用ADO访问目录(添加对Microsoft ActiveX Data Objects 2.x的引用):
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql As String
cn.Open "Provider=Search.CollatorDSO;Extended Properties='Application=Windows'"
sql = "SELECT System.ItemNameDisplay,System.ItemPathDisplay FROM SystemIndex WHERE SCOPE='file:C:\look\here' AND System.Kind <> 'folder' AND CONTAINS(System.FileName,'""*.PDF""') AND CONTAINS ('""find this text""')"
rs.Open sql,cn,adOpenForwardOnly,adLockReadOnly
If Not rs.EOF Then
Do While Not rs.EOF
Debug.Print "File: "; rs.Collect(0)
Debug.Print "Path: "; rs.Collect(1)
rs.MoveNext
Loop
End If
,
请尝试使用下一个功能,
Function GetFilteredFiles(foldPath As String) As Collection
'If using a reference to `Microsoft Internet Controls (ShDocVW.dll)_____________________
'uncomment the next 2 lines and comment the following three (without any reference part)
'Dim ExpWin As SHDocVw.ShellWindows,CurrWin As SHDocVw.InternetExplorer
'Set ExpWin = New SHDocVw.ShellWindows
'_______________________________________________________________________________________
'Without any reference:_____________________________________
Dim ExpWin As Object,CurrWin As Object,objshell As Object
Set objshell = CreateObject("Shell.Application")
Set ExpWin = objshell.Windows
'___________________________________________________________
Dim Result As New Collection,oFolderItems As Object,i As Long
Dim CurrSelFile As String
For Each CurrWin In ExpWin
If Not CurrWin.Document Is Nothing Then
If Not CurrWin.Document.FocusedItem Is Nothing Then
If left(CurrWin.Document.FocusedItem.Path,_
InStrRev(CurrWin.Document.FocusedItem.Path,"\")) = foldPath Then
Set oFolderItems = CurrWin.Document.folder.Items
For i = 0 To oFolderItems.count
On Error Resume Next
If Err.Number <> 0 Then
Err.Clear: On Error GoTo 0
Else
Result.Add oFolderItems.item(CLng(i)).Name
On Error GoTo 0
End If
Next
End If
End If
End If
Next CurrWin
Set GetFilteredFiles = Result
End Function
就像这样,该函数无需任何引用即可工作...
在使用现有代码执行搜索查询之后,必须调用上述函数。可以通过以下(测试)方式调用它:
Sub testGetFilteredFiles()
Dim C As Collection,El As Variant
Set C = GetFilteredFiles("C:\Teste VBA Excel\")'use here the folder path you used for searching
For Each El In C
Debug.Print El
Next
End Sub
上述解决方案在所有IExplorer
个窗口之间进行迭代,并返回(在过滤后)在其中可见的内容(),该标记用于您最初用于搜索的文件夹。
您可以手动对其进行测试,在特定文件夹中搜索某些内容,然后使用该特定文件夹路径作为参数来调用函数("\"
末尾反斜杠...)。
我已经忘记了有关 VBA 的所有知识,但最近偶然发现了一种使用 Shell.Application COM 对象执行资源管理器搜索的简单方法。我的代码是 PowerShell,但 COM 对象和方法是关键。这里肯定有人会翻译。
我认为这有几个优点:
-
查询文本与您将在 Explorer 的搜索栏中键入的内容相同,例如'
Ext:pdf Content:compressor
' -
它很容易从代码中启动,结果很容易用代码提取,但
SearchResults
窗口可用于目视检查/审查。 -
通过循环和暂停,您可以在同一个窗口中执行一系列搜索。
我认为这种能力一直存在,但是 Document
对象和 FilterView
方法的 MS 文档没有提到它们如何应用于文件资源管理器。
我希望其他人觉得这很有用。
$FolderToSearch = 'c:\Path\To\Folder'
$SearchBoxText = 'ext:pdf Content:compressor'
$Shell = New-Object -ComObject shell.application
### Get handles of currenlty open Explorer Windows
$CurrentWindows = ( $Shell.Windows() | Where FullName -match 'explorer.exe$' ).HWND
$WinCount = $Shell.Windows().Count
$Shell.Open( $FolderToSearch )
Do { Sleep -m 50 } Until ( $Shell.Windows().Count -gt $WinCount )
$WindowToSerch = ( $Shell.Windows() | Where FullName -match 'explorer.exe$' ) | Where { $_.HWND -notIn $CurrentWindows }
$WindowToSearch.Document.FilterView( $SearchBoxText )
Do { Sleep -m 50 } Until ( $WindowToSearch.ReadyState -eq 4 )
### Fully-qualified name:
$FoundFiles = ( $WindowToSearch.Document.Folder.Items() ).Path
### or just the filename:
$FoundFiles = ( $WindowToSearch.Document.Folder.Items() ).Name
### $FoundFIles is an array of strings containing the names.
### The Excel portion I leave to you! :D
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。