Ryan's answer is nice because it works without a hard reference to the base name of the new file name.
I'm always renaming files for one reason or another and found creative ways to manipulate strings using powershell. Here's another way to do this:
gci *.jpg | Rename-Item -NewName {$_.basename -replace "$($_.basename.split(".")[-1])$","$(290-$_.basename.split(".")[-1])$($_.extension)"}
This one-liner can be reused to rename other files in the same way, whether they're jpgs, gifs, or txt files. Only requirement is that the last part of the basename end with ".#"
So, what's going on here? To break it down, let's say $_ is currently a [System.IO.FileInfo] object for the file "Vitrine Hinge.12.jpg". Therefore:
$_.basename
would equal [string] "Vitrine Hinge.12"
$_.basename.split(".")
would result in an array with the following entries:
Door Hinge
12
As such, [1] would refer to the "12". However, note that I instead used:
$_.basename.split(".")[-1]
This is because if I had a file named, "Viterine Hinge.Chrome.12.jpg", I would have made reference to "Chrome". Using [-1] will give me the last item in the array and therefore it wouldn't matter if my file had other periods in the name.
OK, so now at this point, I encapsulate that in $() and use in in my -replace operator. Note that I end the first argument with $ so that I would regex match at the end of the string. This way, It wouldn't matter if my files had other numbers in the name. For example, "Viterine D00r Knob.0.jpg" would not get renamed to "Viterine D290290r Knob.290.jpg"
Let me know if missed something,
Cheers :)
.
you can use this:gci . *.jpg | % { $int = $_.Name.Split(".")[1] ; $number = ($int - 290) * -1 ; rni $_.FullName -NewName ($_ -replace $int,$number) }
(x-290)*-1
, why not just say290-x
?