python - Rename multiple .docx files in a folder using a script task
问题描述
I have 453 files in a folder (all of type .docx the problem is these files need to be renamed in a proper format
Vendor1_FinanceReport.docx
Vendor5_FinanceReport.docx
Vendor17_FinanceReport.docx
Vendor91_FinanceReport.docx
I want to rename this into something like
Vendor1_FinanceReport_Oct_2020.docx
Vendor5_FinanceReport_Oct_2020.docx
Vendor17_FinanceReport_Oct_2020.docx
Vendor91_FinanceReport_Oct_2020.docx
解决方案
为了安全起见,我Get-ChildItem
在括号之间加上了一行,这样代码就不会对已经重命名的文件进行酸式迭代。
通过添加-File
开关,您可以确保只重命名文件,而不是文件夹。此外,通过使用-Filter
代码只会影响 .docx 文件
$rootFolder = 'path\to\the\folder\where\the\files\are'
$append = '_Oct_2020' # hardcoded as per your comment
(Get-ChildItem -Path $rootFolder -Filter '*.docx' -File) |
Rename-Item -NewName { '{0}{1}{2}' -f $_.BaseName, $append, $_.Extension }
推荐阅读
- sql - 按 Rowid 问题索引
- linux - 错误 TS2420:类 'NgRedux
' 错误地实现了接口 'ObservableStore ' - angular - 为什么订阅函数的回调中的对象一直未定义?[解决了]
- cluster-analysis - 如何指定集群的大小?
- python - 如何在 PySimpleGUI GUI 中接收数据?
- spring-integration - 为什么我的动态 IntegrationFlow 没有路由到错误通道?
- excel - 计算宏在 4 次使用后运行和重置的次数
- python-3.x - 使用带有 RDBMS 表的 Redis 实现缓存
- javascript - ReactJS - 嵌套块是多余的非孤立块?
- python - 无法打印正确解码的 readAllStandardOutput