I am trying to use PowerShell
to rename a folder containing thousands of images. I want to add both a prefix and a suffix, but this is proving more difficult than I bargained for. I don't understand PowerShell at all... and have dug around for help, and ended up with mixed results.
The best I've found was the example here: Renaming Files with PowerShell
Which yields ...
Dir -recurse | Sort {$.FullName.Length} -Desc | Rename-Item {$.Name -replace ' ','_'}
Now, I'm not wanting to replace anything, so I tried to just
Dir -recurse | Sort {$.FullName.Length} -Desc | Rename-Item { "pre-" + $.Name + "-suff" }
That gave me an error though, and I'm not quite sure how to interpret it;
Rename-Item : A positional parameter cannot be found that accepts argument ' "pre-" + $_.Name + "-suff" '.
At line:1 char:50
+ ... _.FullName.Length} -Desc | Rename-Item { "pre-" + $_.Name + "-suff" }
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Rename-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RenameItemCommand
I quite literally have no idea what this is trying to say. I've dug around deeper, and found this tutorial: http://blogs.technet.com/b/heyscriptingguy/archive/2013/11/22/use-powershell-to-rename-files-in-bulk.aspx
And it has some slightly different commands, so I tried to plug that in with...
Get-ChildItem -Filter "current" -Recurse | Rename-Item -NewName { "pre-" + $_.name + "-suffix" }
And I get no error, but nothing seems to happen, either. This is proving to be very frustrating, as I keep searching for answers but every page I turn up is nebulous at best. Is there a simpler way to accomplish this? I'm wanting to append the prefix and suffix without affecting the file extension.
I did find something a bit closer to what I needed at: http://social.technet.microsoft.com/wiki/contents/articles/25144.powershell-bulk-renaming-file-names.aspx
And it suggested this script,
Get-ChildItem | Where-Object { $.Extension -eq ".jpg" -or $.Extension -eq ".png"} | rename-item -newname {"CL - " + $_.Name}
Which I changed to ...
Get-ChildItem | Where-Object { $.Extension -eq ".jpg" -or $.Extension -eq ".png"} | rename-item -newname {"prefix-" + $_.Name + "-suffix" }
And that at least did something to the files, but now the suffix is appended after the extension, which is obviously not what I'm going for.
{"prefix-" + $_.Name + "-suffix"}
to{"prefix-$($_.BaseName)-suffix$($_.Extension)"
– Lieven Keersmaekers