powershell - 在 Powershell 中批量重命名照片并向重复文件名添加字母

标签 powershell rename

我有一个关于 powershell 脚本的问题。我想重命名文件夹中的一堆照片。我有一个包含旧名称和新名称的 .csv 文件。这是该文件的一部分:

OldFile NewFile
{5858AA5A-DB1B-475A-808E-0BFF0B885E5B}.jpeg 975NNNN-AGUIRRESUGARASSOCSTACK-Notes-20200828.jpeg
{FA1E4CEE-0AD8-4B40-A5AD-4BB22C0EE4F0}.jpeg 975NNNN-AGUIRRESUGARASSOCSTACK-Other-20200828.jpeg
{FD20FA44-B3D2-4A6A-B73D-F3BADC2DDE71}.jpeg 975NNNN-AGUIRRESUGARASSOCSTACK-Vicinity-20200831.jpeg
{E0DDA4CD-7783-417C-9BE0-705FFA08CD17}.jpeg 975NNNN-AGUIRRESUGARASSOCSTACK-Vicinity-20200831.jpeg
{76DC6315-942D-444C-BA04-92FC9B9FF1A5}.jpeg 975NNNN-AGUIRRESUGARASSOCSTACK-Vicinity-20200831.jpeg
{3C853453-0A0D-40B5-B3B7-B0F84F92D512}.jpeg 975NNNN-AGUIRRESUGARASSOCSTACK-Vicinity-20200831.jpeg

Many of the new file names will be duplicates. For those files, I want to add a letter (A,B,C, so on) in the middle of the name at an exact location.

For example, if the file, 975NNNN-AGUIRRESUGARASSOCSTACK-Vicinity-20200831.jpeg, is a duplicate, I want to add "A" right after "Vicinity", so that the file is called 975NNNN-AGUIRRESUGARASSOCSTACK-VicinityA-20200831.jpeg. The letter will always be at that exact same location (right before the third -).

This is the script I have so far. I know it's not right and I haven't been able to even attempt at adding the letter within the script. (I'm a complete Powershell newbie.)

$filesToRename = Import-CSV C:\Users\clair\OneDrive\Documents\JOA\batch_photos\Rename_Central_Aguirre.csv   
foreach ($file In $filesToRename) {
    if (Test-Path $file.NewFile) {
        $letter = -begin { $count= 1 } -Process { Rename-Item $file.OldFile
            "file-$([char](96 + $count)).jpeg"; $count++}
    } else {
        Rename-Item $file.OldFile $file.NewFile
    }
}

我可以获得一些有关如何实现此文件命名系统的指导吗?

谢谢!!!

最佳答案

当使用字母表中的字符重命名文件时,意味着您只有 26 个选项。如果这对您来说足够了,您可以执行以下操作:

$alphabet      = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
$folderPath    = 'D:\Test'
$filesToRename = Import-CSV C:\Users\clair\OneDrive\Documents\JOA\batch_photos\Rename_Central_Aguirre.csv

foreach ($file In $filesToRename) {
    $oldFile = Join-Path -Path $folderPath -ChildPath $file.OldFile
    if (Test-Path $oldFile -PathType Leaf) {
        # split the new filename into workable parts
        $newName = $file.NewFile
        $extension = [System.IO.Path]::GetExtension($newName)
        $parts = [System.IO.Path]::GetFileNameWithoutExtension($newName) -split '-'
        $suffix = $parts[-1]
        $prefix = $parts[0..($parts.Count -2)] -join '-'
        $charToAppend = 0    # counter to go through the characters in the alphabet. 0..25
        while (Test-Path (Join-Path -Path $folderPath -ChildPath $newName) -PathType Leaf) {
            if ($charToAppend -gt 25) {
                # bail out if al characters have been used up
                throw "Cannot rename file '$($file.OldFile)', because all characters A-Z are already used"
            }
            $newName = '{0}{1}-{2}{3}' -f $prefix, $alphabet[$charToAppend++], $suffix, $extension
        } 
        Rename-Item -Path $oldFile -NewName $newName
    }
    else {
        Write-Warning "File '$($file.OldFile)' not found"
    }
}

之前:

D:\TEST
    {3C853453-0A0D-40B5-B3B7-B0F84F92D512}.jpeg
    {5858AA5A-DB1B-475A-808E-0BFF0B885E5B}.jpeg
    {76DC6315-942D-444C-BA04-92FC9B9FF1A5}.jpeg
    {E0DDA4CD-7783-417C-9BE0-705FFA08CD17}.jpeg
    {FA1E4CEE-0AD8-4B40-A5AD-4BB22C0EE4F0}.jpeg
    {FD20FA44-B3D2-4A6A-B73D-F3BADC2DDE71}.jpeg

之后:

D:\TEST
    975NNNN-AGUIRRESUGARASSOCSTACK-Notes-20200828.jpeg
    975NNNN-AGUIRRESUGARASSOCSTACK-Other-20200828.jpeg
    975NNNN-AGUIRRESUGARASSOCSTACK-Vicinity-20200831.jpeg
    975NNNN-AGUIRRESUGARASSOCSTACK-VicinityA-20200831.jpeg
    975NNNN-AGUIRRESUGARASSOCSTACK-VicinityB-20200831.jpeg
    975NNNN-AGUIRRESUGARASSOCSTACK-VicinityC-20200831.jpeg

关于powershell - 在 Powershell 中批量重命名照片并向重复文件名添加字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63783472/

相关文章:

powershell - emacs Powershell与Powershell模式

powershell - 从 C# 转换为 Powershell 语法

.net - 为什么我的 Write-Verbose 消息没有出现?

Powershell 使用通配符重命名文件

powershell - 为什么是Get-DnsClientServerAddress |选择AddressFamily输出不是IPv4和IPv6

xml - 使用 PowerShell 在 Xml 中添加元素

Python os.rename() 用于父目录中多个子文件夹中的文件

elasticsearch - 重命名和删除 Elasticsearch 索引

linux - 在 Ubuntu 中将所有子目录中的文件名更改为小写

linux - 通过删除扩展名后的字符来重命名 Linux 文件