我需要幫助才能在 PowerShell 中創建一個命令來重命名具有這種格式的檔案序列:
001.jpg 001_1.jpg 002.jpg 002_1.jpg 003.jpg 003_1.jpg
進入一個新序列,該序列可以以 9612449 等數字開頭,但后綴保持不變,因此新序列將是:
9612449.jpg 9612449_1.jpg 9612450.jpg 9612450_1.jpg 9612451.jpg 9612451_1.jpg
uj5u.com熱心網友回復:
假設這9612449是要添加到構成第一個- 分隔標記或所有基本檔案名的現有數字的偏移量:_
# Simulate a set of input files.
$files = [System.IO.FileInfo[]] (
'001.jpg',
'001_1.jpg',
'002.jpg',
'002_1.jpg',
'003.jpg',
'003_1.jpg'
)
# The number to offset the existing numbers by.
$offset = 9612449 - 1
# Process each file and apply the offset.
$files | ForEach-Object {
# Split the base name into the number and the suffix.
$num, $suffix = $_.BaseName -split '(?=_)', 2
# Construct and output the new name, with the offset applied.
'{0}{1}{2}' -f ($offset $num), $suffix, $_.Extension
}
以上產生了您的問題中顯示的輸出。
應用于真正的檔案重命名操作,您會執行以下操作:
Get-ChildItem -LiteralPath . -Filter *.jpg |
Rename-Item -NewName {
$num, $suffix = $_.BaseName -split '(?=_)', 2
'{0}{1}{2}' -f ($offset $num), $suffix, $_.Extension
} -WhatIf
注意:上面命令中的-WhatIf常用引數是預覽操作。-WhatIf 一旦您確定該操作將執行您想要的操作,請洗掉。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/412560.html
標籤:
下一篇:PowerShell腳本的輸出
