使用PowerShell在名称中间的数字重命名文件

发布于 2025-02-13 22:35:55 字数 1082 浏览 2 评论 0原文

我为此搜索了很长时间,但似乎无法找到在多个目录中完成文件重命名的手段。问题是我需要在文件名的中间插入一个唯一的数字序列。以下是我的文件名的示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2025-02-20 22:35:58

由于您的文件名似乎具有结构或独特的字符,因此您可以将它们分开,您可以使用类似的内容:

$i = 1 
Get-ChildItem -Filter *.html | 
ForEach-Object{
    $SplitName = $_.BaseName -split '_'
    $NewName = ((@($SplitName[0]) * 2) -join '_') + '_' + ('{0:D5}' -f $i) + '_2022' + ($SplitName[1..($SplitName.Length -1)] -join '_') + $_.Extension 
    Rename-Item -Path $_.FullName -NewName $NewName
    $i++
}

我很好奇:为什么您需要两次相同的数字序列?

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 = 1 
Get-ChildItem -Filter *.html | 
ForEach-Object{
    $SplitName = $_.BaseName -split '_'
    $NewName = ((@($SplitName[0]) * 2) -join '_') + '_' + ('{0:D5}' -f $i) + '_2022' + ($SplitName[1..($SplitName.Length -1)] -join '_') + $_.Extension 
    Rename-Item -Path $_.FullName -NewName $NewName
    $i++
}

I'm curious: why do you need the same sequence of numbers twice?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文