Bulk renaming photos and adding letters to duplicate file names in Powershell

Viewed 177

I have a question about a powershell script. I want to rename a bunch of photos within a folder. I have a .csv file of the old names and the new names. This is a section of that file:

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
    }
}

Could I get some guidance on how to achieve this file naming system?

Thanks!!!

2 Answers

When renaming files using a character from the alphabet will mean you will only have 26 options. If that is enough for you, you can do the following:

$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"
    }
}

Before:

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

After:

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

I think you need to use the method.Insert(). This is a small example how it works:

I ve created a txt named 975NNNN-AGUIRRERM1-Vicinity-20200829.txt in C:\Test just for testing purpose, in your example the first code line is to identify the duplicate(s)

#Code to identify duplicates (insert your code instead of mine)
$files=Get-ChildItem -Path C:\Test -File  -Name

#The following line indentifies the location of the last "-" (I understand you always have 3 "-" right?)
$DashPos=($files).LastIndexOf("-")

#This inserts on the position $DashPos, the letter "A")
$files.Insert($DashPos,"A")
Related