使用PowerShell在名称中间的数字重命名文件
我为此搜索了很长时间,但似乎无法找到在多个目录中完成文件重命名的手段。问题是我需要在文件名的中间插入一个唯一的数字序列。以下是我的文件名的示例:
8675309_FileName1_ExtraSuff1_Extrastuff2 and some more stuff_012345.html
2020202_FileName2_ExtraSuff1_Extrastuff2 and some more stuff_012345.html
... etc.
重命名以插入1st 7字段的副本,数字测序和日期如下:
8675309_8675309_00001_2022_FileName1_ExtraSuff1_Extrastuff2 and some more stuff_012345.html
2020202_2020202_00002_2022_FileName2_ExtraSuff1_Extrastuff2 and some more stuff_012345.html
... etc.
这是我到目前为止尝试的:
$ i = 1 Get -Childitem -filter *.html | foreach-object { rename -item -path $ .fullname -newName((($ .basename -split'')[0] +'' +('{0:d5}' - f $ i) +' 2022' +' performance_eval ' +($ .basename -split'')[1] +($ .basename -split'')[2] +($ .basename -split'')[3] +'' +($) .basename -split'')[4] + $_。Extension) $ i ++ }
这给了我大部分我需要的东西,但没有其他前7个字符:
8675309_00001_2022_FILENAME1_EXTRASUFF1_EXTRASTUSTUFC2和其他一些STECK_012345.html
HO,我可以调整此脚本来带来初始字符吗?
I've searched long and hard for this, but cannot seem to locate a means of completing the renaming of files within multiple directories. The issue is that I need to insert a unique number sequence in the middle of a file name. Here are the examples of my file names:
8675309_FileName1_ExtraSuff1_Extrastuff2 and some more stuff_012345.html
2020202_FileName2_ExtraSuff1_Extrastuff2 and some more stuff_012345.html
... etc.
Renamed to insert a copy of the 1st 7 fields, a numeric sequencing and a date as in the following:
8675309_8675309_00001_2022_FileName1_ExtraSuff1_Extrastuff2 and some more stuff_012345.html
2020202_2020202_00002_2022_FileName2_ExtraSuff1_Extrastuff2 and some more stuff_012345.html
... etc.
Here is what I've tried so far:
$i = 1
Get-ChildItem -Filter *.html |
ForEach-Object{
Rename-Item -Path $.FullName -NewName (($.BaseName -split '')[0] + '' + ('{0:D5}' -f $i) + '2022' + 'Performance_Eval' + ($.BaseName -split '')[1] + ($.BaseName -split '')[2] + ($.BaseName -split '')[3] + '' + ($.BaseName -split '')[4] + $_.Extension )
$i++
}
This gives me most of what I need, but not the additional first 7 characters:
8675309_00001_2022_FileName1_ExtraSuff1_Extrastuff2 and some more stuff_012345.html
Ho can I tweak this script to bring in the initial characters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您的文件名似乎具有结构或独特的字符,因此您可以将它们分开,您可以使用类似的内容:
我很好奇:为什么您需要两次相同的数字序列?
Since your file names seem to have a structure or a unique character you can split them on you could use something like this:
I'm curious: why do you need the same sequence of numbers twice?