I have a bunch of image files that accidentally got renamed with a “doubled” file extension (e.g. IMG_1469.jpg.jpg
). I wrote a script to remove the extra file extension, but for some reason, it functions differently based on whether the file extension is in all caps or all lowercase. It functions as intended if the extension is lowercase.
Given two files in the same folder with the following filenames:
$FileA = IMG_1468.JPG.JPG
$FileB = IMG_1469.jpg.jpg
$file = Get-ChildItem -Path "\UNCPathToFile" | Where-Object -Property Name -EQ $FileA
If (($file.BaseName).Length -GT 3) {
$extCheck = ($file.BaseName).Substring(($file.BaseName).Length - 4,4)
Write-Host "extCheck =" $extCheck
If ($file.Extension -EQ $extCheck) {
$oldName = $file.Name
Write-Host "oldName =" $oldName
Write-Host "File.Extension =" $file.Extension
$newName = ($oldName).Split($extCheck)[0]+$file.Extension
Write-Host "newName =" $newName
#$file | Rename-Item -NewName { $newName }
}
}
Output using $FileA:
extCheck = .JPG
oldName = IMG_1468.JPG.JPG
File.Extension = .JPG
newName = IM.JPG
Output using $FileB:
extCheck = .jpg
oldName = IMG_1469.jpg.jpg
File.Extension = .jpg
newName = IMG_1469.jpg
Anyone have any idea why .Split is removing everything from the base name after “IM” instead of just removing .JPG
like it does when the extra extension is .jpg
?