-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?

Compo
  • 36,585
  • 5
  • 27
  • 39
Robin
  • 3
  • 2
  • Does this answer your question? [Batch Script - Rename files, removing a variable prefix and suffix](https://stackoverflow.com/questions/35612196/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. – SomethingDark Apr 14 '23 at 20:45

3 Answers3

2

I think this is the simplest way:

for %%a in ("%filename: - =\%") do set "track=%%~Na"
Aacini
  • 65,180
  • 12
  • 72
  • 108
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).

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 1
    [... and then there are titles that contain dots](https://en.wikipedia.org/wiki/Y.M.C.A._(song)) (sorry for tainting your answer) – Stephan Apr 15 '23 at 07:04
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
Compo
  • 36,585
  • 5
  • 27
  • 39