首页 > 解决方案 > 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

标签: pythonwindowspowershell

解决方案


为了安全起见,我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 }

推荐阅读