1

I have a few hundred files in file explorer that I am looking to rename. All of the files end with a set of 2 characters after the extension. EX: 23RH1.JPG;1 Is there a way to batch rename all of the files by removing the last 2 characters from all of the file names? Or maybe a Powershell script I could run that would auto remove last 2 characters from end of all the file names?

Thank You in advance

so far i have tried running something along the lines of

get-childitem *.JPG | rename-item -newname { $_.fullname.substring(0,$_.fullname.length-2) + $_.extension }

and

gci <dir path \*> |% {rename-item $_.fullname -newname $_.basename -whatif}

Unfortunately my PowerShell knowledge is quite limited and I'm not really sure how to mass rename a ton of files at once as its a fairly new tool to me.

Huge thank you to anyone that could offer any help or advice.

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37

2 Answers2

2

In this case the recommendation would be to refine your filter to find only those files ending with .jpg followed by ; and anything after, then for renaming, you could use a simple regex that replaces the last 2 characters from a string .{2}$:

Get-ChildItem path\to\targetfolder -Filter '*.jpg;?' |
    Rename-Item -NewName { $_.Name -replace '.{2}$' }
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
0

Give this a shot

Get-ChildItem -Path "C:\Path\To\Your\Folder" | foreach {Rename-Item $_.FullName -NewName ($_.BaseName.Substring(0, $_.BaseName.Length - 2) + $_.Extension)}