Powershell Rename Files by CSV - Multiple same name match

Viewed 46

So I found this powershell script to rename files in a dir based on the csv table. It works as long as there is only one file with the exact name in the table. Well I have multiple files matching the same name that need replacing based on the table, example: jackson_001.jpg jackson_002.jpg, ect.. and if jackson is in the table with a matching rename cell, it does not recognize. I have tried .baseline.length -4) and that does nothing. Do I have to do more than just truncate the basename?

$csv    = Import-Csv "path\to\rename.csv"

# location of your files
$files = get-childitem "path\to\myimages"


foreach($item in $CSV){
    foreach($file in $files){
        if($item.oldName -eq $file.basename){
            rename-item $file.fullname -NewName "$($item.MyNewName)_Preview_$($item.oldName)-$($item.MyNewName)$($file.extension)" -Verbose
        }
    }
}

0 Answers
Related