0

This maybe simple enough, but I am not that of an expert to computer languages. I've been stuck searching for a solution for almost 3 hours on the internet.

Assuming all my mp3 files are titled with "Name of Artist - Title of Song.mp3" I would want it to output into a txt file that contants:
Artist: Name of Artist
Song: Title of Song

How do I parse the file name into two parts separated with a hyphen? I've been trying to do some sort of automation with batch files for archiving purposes and here's my code where I'm stuck with:

@echo off
for /r %%a in (*.mp3) do (
(
for %%b in ("%%~na") do echo ^Artist: %%~b
echo ^Song:
)>"%%~dpna.txt"
) 
  • Use a `for /f` to parse the file name [read here](http://ss64.com/nt/for_f.html) . You want a text file for every single mp3? –  Jun 03 '18 at 13:15
  • @LotPings Yes, that is right. I'm trying to understand the link you gave me but it seems a bit overwhelming. Can you expand on how it would come into play? – Mr-RightHanded Jun 03 '18 at 13:24

2 Answers2

1

How do I parse the file name into two parts separated with a hyphen?

I would want it to output into a txt file that contains:

Artist: Name of Artist
Song: Title of Song

Use the following batch file as a starting point:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=-" %%i in ('dir /b name*') do (
  echo Artist: %%i 
  echo Song: %%j
 )>>file.txt
endlocal

Example usage:

> dir name*
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test

03/06/2018  14:06                 0 Name of Artist - Title of Song.mp3
03/06/2018  14:07                 0 Name of Artist 1 - Title of Song 1.mp3
               2 File(s)              0 bytes
               0 Dir(s)  1,269,011,574,784 bytes free

> test

> type file.txt
Artist: Name of Artist
Song:  Title of Song.mp3
Artist: Name of Artist 1
Song:  Title of Song 1.mp3    
>

I would want it to have a single text file for every mp3 file. Is that possible?

Yes Use the following batch file:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('dir /b name*.mp3') do (
  set _filename=%%~dpna.txt
  for /f "tokens=1,2 delims=-" %%i in ("%%a") do (
    echo Artist: %%i 
    echo Song: %%j
    )>!_filename!
  )
endlocal

Example usage:

> dir *.mp3
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test

03/06/2018  14:06                 0 Name of Artist - Title of Song.mp3
03/06/2018  14:07                 0 Name of Artist 1 - Title of Song 1.mp3
               2 File(s)              0 bytes
               0 Dir(s)  1,269,022,654,464 bytes free

> test

> type name*.txt

Name of Artist - Title of Song.txt


Artist: Name of Artist 1
Song:  Title of Song 1.mp3

Name of Artist 1 - Title of Song 1.txt


Artist: Name of Artist 1
Song:  Title of Song 1.mp3

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
  • Thank you for help @DavidPostill but I would want it to have a single text file for every mp3 file. Is that possible? – Mr-RightHanded Jun 03 '18 at 13:29
  • @Mr-RightHanded Well then edit the question to specify how you want the text file to be named. – DavidPostill Jun 03 '18 at 13:30
  • Uuh ooh, no quoting of file names known to contain spaces. Why save to variable which enforces delayed expansion? In case the file name contains another dash, the remainder is truncated. –  Jun 03 '18 at 14:12
  • @LotPings I tested it **specifically** with file names that contained spaces ... if you bother to read the answer properly you can see that in the "Example usage" – DavidPostill Jun 03 '18 at 14:15
  • @LotPings There is nothing wrong with delayed expansion. If only more people knew what it was and how it works ... – DavidPostill Jun 03 '18 at 14:16
  • I agree with the delayedexpansion but the vanishing of `!` in processed text is a reason to use it only when necessary. Your batch works despite not quoting on redirection which astonished me. OTH the extension is in the song and it does not recurse. –  Jun 03 '18 at 14:23
0

Change the startfolder following PushD to fit your environment.

:: Q:\Test\2018\06\03\SO_50666632.cmd
@echo off
PushD "%USERPROFILE%\Music" || (Echo can't locate folder&Pause&exit /B 1)

for /r %%a in (*.mp3) do (
    if exist "%%~dpna.txt" (
        Echo "%%~dpna.txt" already present, skip
    ) else (
       for /f "tokens=1,*delims=-" %%b in ("%%~na") do (
           echo Artist: %%b
           echo Song  :%%c
       )>"%%~dpna.txt"
    )
)

Sample output on my ramdrive a:

> tree /F
Auflistung der Ordnerpfade
Volumeseriennummer : 5566-7788
A:.
│   Name of Artist - Title of Song.mp3
│   Name of Artist - Title of Song.txt
│
└───Music
        Survivor - Eye of the Tiger.mp3
        Survivor - Eye of the Tiger.txt


> type "Name of Artist - Title of Song.txt"
Artist: Name of Artist
Song  : Title of Song

> type "Music\Survivor - Eye of the Tiger.txt"
Artist: Survivor
Song  : Eye of the Tiger
  • That did the trick. Thank you I was actually trying to add for /f into a different line. Not much of an expert, like I said. Thanks. Saved me a lot of time. Cheers! – Mr-RightHanded Jun 03 '18 at 13:36
  • Btw, follow up question regarding this issue. Will this code skip existing files? Like, updated music library. Would that be a problem with thousands of songs etc..? – Mr-RightHanded Jun 03 '18 at 13:59
  • No at present it simply overwrites existing files. Enhanced answer to first check. –  Jun 03 '18 at 14:04