从文件资源管理器搜索中提取文件名到Excel

如何解决从文件资源管理器搜索中提取文件名到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 对象和方法是关键。这里肯定有人会翻译。

我认为这有几个优点:

  1. 查询文本与您将在 Explorer 的搜索栏中键入的内容相同,例如'Ext:pdf Content:compressor'

  2. 它很容易从代码中启动,结果很容易用代码提取,但 SearchResults 窗口可用于目视检查/审查。

  3. 通过循环和暂停,您可以在同一个窗口中执行一系列搜索。

我认为这种能力一直存在,但是 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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-