win10脚本 | 使用 Word 自动化对象模型找出指定路径下含有特定内容的.docx

场景

今年的实验日志被我放在这样一个文件夹下,每个月下是每天具体的.docx文件,里面记录了我的一些实验操作步骤。现在我需要补充一个实验,用到一个名为chatunitest的插件,但是这是很久之前做的事情了,我无法判断是哪个月哪一天。所以我需要一个能够在powershell中执行的脚本,帮助我找到日志,复用之前的操作步骤。

脚本

执行条件:安装了word就行

bash 复制代码
$directoryPath = "C:\实验日志\2023"
$searchKeyword = "chatunitest"
$word = New-Object -ComObject Word.Application
$word.Visible = $false

Get-ChildItem -Path $directoryPath -Recurse -Filter *.docx | ForEach-Object {
    $document = $word.Documents.Open($_.FullName)
    $text = $document.Range().Text
    $document.Close()

    if ($text -match $searchKeyword) {
        Write-Output "Found in file: $($_.FullName)"
    }
}

$word.Quit()

执行结果

成功找了包含chatunitest的日志,并返回了它们的路径。

相关推荐
名字还没想好☜8 小时前
Python itertools 实战:用 groupby、chain、islice 优雅处理大数据流
linux·windows·python·迭代器·itertools
ziguo112211 小时前
深入浅出 C/C++ 数据类型:从入门到踩坑
linux·c语言·c++·windows·visual studio
阿扬ABCD12 小时前
ipmitool的Windows版本编译
windows·驱动开发
王维同学15 小时前
Credential Provider、Filter 与 PLAP 的 CLSID 枚举
c++·windows·安全·注册表
寒水馨17 小时前
Windows下载、安装 Codex CLI(附安装包codex-x86_64-pc-windows-msvc.exe)
windows·rust·codex·终端工具·自动化编程·codex cli·ai编码代理
Sunflower0218 小时前
如何解决word/WPS word 中表格跳到下一页(上一页有大篇幅空白)
word·wps
其实防守也摸鱼20 小时前
镜像校验完成iso完整操作流程(Windows 环境,适配 Ubuntu 22.04 / 24.04)
linux·服务器·windows·学习·ubuntu·教程
像风一样自由20201 天前
从本地到公网:Windows 下使用 Cloudflare Quick Tunnel 与 Natapp 联调 FastAPI
windows·fastapi
2601_960906721 天前
科技“无人区”跋涉,难度高、不确定性大
windows·macos·pycharm·myeclipse
会飞的大鱼人1 天前
一文搞懂 Java HashSet:把它想成游乐园里只允许一次入场的盖章名单
java·开发语言·windows