0

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:

(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.

1
  • if you read the questions you linked above carefully you'll see this answer that preserves the extension
    – phuclv
    Commented Mar 11, 2022 at 2:01

2 Answers 2

1

See if it is this you want:

@echo off
setlocal EnableDelayedExpansion
for %%a in (*.mp3) do (
                       set "SName=%%~na"
                       ren "%%a" "!SName:~0,-3!%%~xa"
                      )
exit
3
  • Thanks, but that also removes the dot and the file extension. EDIT: Wait no sorry it does work! I was still running my old command sorry sorry sorry.
    – bat_cmd
    Commented Mar 11, 2022 at 1:09
  • have you tested it? For me it doesn't Commented Mar 11, 2022 at 1:10
  • It does work sorry, I was accidentally still running my old command before it. Thanks for answering this so quickly!
    – bat_cmd
    Commented Mar 11, 2022 at 1:11
1

You're removing the last 7 characters so obviously the extension will be removed. You should only replace the BaseName

gci *.mp3 | rename-item -newname { $_.basename.substring(0,$_.basename.length-3) + $_.extension }
2
  • Cheers, this also works and since it's a one liner I'll use it. Always good to have both options :)
    – bat_cmd
    Commented Mar 11, 2022 at 3:02
  • And, just for grins, we try to go shorter: gci *.mp3 | Rename-Item -NewName { '{0}.mp3' -f ( $_.BaseName -replace '.{3}$' ) } or the even shorter (but IMHO, less scanable): gci *.mp3 | Rename-Item -NewName { $_.Name -replace '(?=.{3}\.mp3$).{3}' } Commented Mar 11, 2022 at 9:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .