-2

I am writing a batch file to convert music files and have saved a file name in a variable. A couple of examples would be: David Bowie - Space Oddity.mp3 Or maybe David Bowie - Greatest Hits - Space Oddity.mp3

I'd like to edit the variable so it just contains the track name Space Oddity. So I need to search backwards from the end until I reach the first occurrence of {Hyphen}{Space}, and discard everything that comes before it, (since the variable might contain more than one hyphen).

Please could you suggest some batch code to achieve this?

1
  • Does this answer your question? Batch Script - Rename files, removing a variable prefix and suffix Specifically, you need substring replacement with a wildcard. It will look something like set "mp3_name=%mp3_name:*- =%" and you will have to do this more than once depending on how many instances of ` - ` you want to remove. Also, for renaming music files, I'd recommend existing software like FileBot or TagScanner. Commented Apr 14, 2023 at 20:45

3 Answers 3

2

I think this is the simplest way:

for %%a in ("%filename: - =\%") do set "track=%%~Na"
Sign up to request clarification or add additional context in comments.

Comments

1
@ECHO OFF
SETLOCAL

SET "var=David Bowie - Space Oddity.mp3"
FOR %%e IN ("%var%") DO SET "var=%%~ne"
FOR %%e IN ("%var:- =.%") DO SET "var=%%~xe"
SET "var=%var:~1%"
SET var
ECHO ---------------
SET "var=David Bowie - Greatest Hits - Space Oddity.mp3"
FOR %%e IN ("%var%") DO SET "var=%%~ne"
FOR %%e IN ("%var:- =.%") DO SET "var=%%~xe"
SET "var=%var:~1%"
SET var
ECHO ---------------
SET "var=Voices in the Sky - The Best of the Moody Blues The Moody Blues 05 - I'm Just a Singer (In a Rock and Roll Band).mp3"
FOR %%e IN ("%var%") DO SET "var=%%~ne"
FOR %%e IN ("%var:- =.%") DO SET "var=%%~xe"
SET "var=%var:~1%"
SET var

GOTO :EOF

The first for removes the extension; the second modifies each - to . and then grabs the extension and the final set removes the leading ..

All based on for /? from the prompt - using the string as a filename and using standard interpretations of what constitutes the various parts of a filename (drive, path, name and extension).

1 Comment

... and then there are titles that contain dots (sorry for tainting your answer)
0

You could get the assistance of a more complete scripting language, such as PowerShell:

Examples:

@Set "FileName=David Bowie - Space Oddity.mp3"
@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "('%FileName%' -Split '- ')[-1]"
@Pause
@Set "FileName=David Bowie - Greatest Hits - Space Oddity.mp3"
@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "('%FileName%' -Split '- ')[-1]"
@Pause

If you wanted the substring to be re-propagated into the variable name then use a FOR /F loop.

Example:

@Set "FileName=David Bowie - Greatest Hits - Space Oddity.mp3"
@For /F "EOL=? Delims=" %%G In ('
 %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command
 "('%FileName%' -Split '- ')[-1]"') Do @Set "FileName=%%~nG"
@Set Filename
@Pause

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.