gpt4 book ai didi

powershell - 重命名项目 : add unique numeric sequence to prevent duplicate file names

转载 作者:行者123 更新时间:2023-12-02 23:38:42 24 4
gpt4 key购买 nike

我需要一种方法来重命名目录中的文件,这会导致简单的错误 Rename-Item .文件需要相同的 10 位代码,后跟 4 位占位符(请仅提供数字)。

当前文件名:

01_img_0028.JPG01_img_0029.JPG02_img_0028.JPG02_img_0029.JPG

Considering the files above, renaming the files with a split (using the 4 digit in the original name) would fail because there will be files with the same name:

B0000000000.0028.JPGB0000000000.0029.JPGB0000000000.0028.JPGB0000000000.0029.JPG

Does anyone have an idea to get around this? The 4 digits can be any sequence of numbers but it would be great if we could make the end result look like:

B0000000000.0001.JPGB0000000000.0002.JPGB0000000000.0003.JPGB0000000000.0004.JPG

Here is my current code that will rename all unique files and the first of duplicates, but error out on files that would then be duplicate names:

$jpgToRename = GCI -Path $pathToRename -Filter '*.jpg' -R
foreach ($jpg in $jpgToRename) {
$splitPath = $jpg.FullName.Split("\\")
$newName = -join ($splitPath[7], ".", $jpg.BaseName, ".PC_850.jpg")
Rename-Item $jpg.FullName -NewName $newName
}

最佳答案

在这里使用计数器可以满足您的需要:

$jpgToRename = GCI -Path $pathToRename -Filter '*.jpg' -R
$counter = 1
foreach($jpg in $jpgToRename){
$splitPath = $jpg.FullName.Split("\\")
$formattedCounter = $counter.ToString("0000")
$newName = -Join($splitPath[7], ".",$formattedCounter, $jpg.BaseName, ".PC_850.jpg")
Rename-Item $jpg.FullName -NewName $newName
$counter++
}

关于powershell - 重命名项目 : add unique numeric sequence to prevent duplicate file names,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48121645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com