Search code examples
windowsbatch-fileplaylistm3u

Constant string in dynamic batch script


I have a batch script (-> windows batch .bat automatic .m3u playlist creation / update ) to automatically generate playlists. These playlists are saved in a subfolder of the rootdir (see code). So the problem is, that the subfolder is not taken into account in the playlist, so the music files cannot be found. I get

#EXTINF:???,The Realm Of Orzamar
Dragon Age Origins\The Realm Of Orzamar.mp3

I need

#EXTINF:???,The Realm Of Orzamar
../Dragon Age Origins\The Realm Of Orzamar.mp3

And with something like

SET "location='.../' + %%c"

I get

#EXTINF:???,The Realm Of Orzamar
:\music\Dragon Age Origins\The Realm Of Orzamar.mp3

How can I put this .../ string into SET "location=%%c" so it won't be converted to music\ . An absolut path is not quite a solution since the filestructure is placed on a portable drive and should work independant and out of the folder root, so for use on Android devices and on any other device with different absolut full paths. I am unexperienced with batch, just did some very basic stuff so far, I don't really understand this script since I don't know batch syntax.

Thanks in advance.

Code:

CHCP 1252
@ECHO Off
SETLOCAL

:: make a tempfile
:maketemp
SET "tempfile=%temp%\%random%"
IF EXIST "%tempfile%*" (GOTO maketemp) ELSE (ECHO.>"%tempfile%a")



:: Need the start of the tree to scan
SET "rootdir=H:\music"
>"%tempfile%b" (ECHO(%rootdir%&ECHO(*)
FOR /f "delims=:" %%a IN ('FINDSTR /o /L "*" "%tempfile%b"') DO SET /a len=%%a-1

SET "destfile=%~1"

:loop
SHIFT
SET nextdir=%~1
IF NOT DEFINED nextdir GOTO process
IF %nextdir:~0,1%==\ (
 PUSHD "%~1"
 FOR /f "delims=" %%a IN ('dir /b /s /a-d *.mp3 *.mp4 *.ogg *.m4a *.wma *.flac *.wav') DO >>"%tempfile%a" ECHO(%%~na:2:%%a
) else (
 PUSHD "%rootdir%\%~1"
 FOR /f "delims=" %%a IN ('dir /b /s /a-d *.mp3 *.mp4 *.ogg *.m4a *.wma *.flac *.wav') DO >>"%tempfile%a" ECHO(%%~na:%len%:%%a
)
POPD 
GOTO loop

:process
>%destfile% ECHO(#EXTM3U
(
FOR /f "tokens=1,2*delims=:" %%a IN ('SORT "%tempfile%a"') DO (
 ECHO(#EXTINF:???,%%a
 SET "location=%%c"
 SETLOCAL enabledelayedexpansion
  ECHO(!location:~%%b!
 endlocal
)
)>>%destfile%

 del "%tempfile%*"


GOTO :EOF

Solution

  • Solved it myself. I looked at the wrong line.

     SET "location=%%c"
     SETLOCAL enabledelayedexpansion
      set "location=!location:~%%b!"
      set "back=../"
      ECHO(!back!!location!
    

    does the job. !location:~%%b! deletes leading characters from the full absolute path up to the folder root, so I just have to add my string after this