I have mp3 files (with names of different lengths) and want to remove the last three characters before ".mp3" for each mp3 file next to the batch file.
For example foobar043.mp3
needs to become foobar.mp3
This sounds simple but I have tried so many ways to do it (with PowerShell in a batch file) and trying to do it with just batch commands - using answers already here on SuperUser.
My last command got the closest, but it removes the file extension too. This is shortened to just the command itself:
gci *.mp3 | rename-item -newname { $_.name.substring(0,$_.name.length-7) }
Now if there was just some way to retain ".mp3" at the end (or rename the now extensionless file back to .mp3 without renaming folders too!) it would be working fine.
People usually ask to list what's already been tried but this question would end up ten times longer! Put it this way, if you type "powershell remove last characters from file name" into Google, I have tried just about everything I have found up to now.
Just a few examples trying the accepted (and voted) answers:
- https://stackoverflow.com/questions/33255834/powershell-rename-filename-by-removing-the-last-few-characters
- https://stackoverflow.com/questions/57202742/how-to-remove-last-x-number-of-character-from-file-name
- https://stackoverflow.com/questions/29636714/remove-end-of-filename-using-powershell
- https://stackoverflow.com/questions/10396769/powershell-remove-last-3-characters-from-file-name
(along with answers on spiceworks and reddit which I won't link to in case that's not allowed here).
Thanks in advance to anyone that knows how to do this while keeping the file extension.
It's a tough one because, normally I would be able to set the file name (without extension) as a variable, but because PowerShell is going to rename the file, that variable effectively becomes useless.