Page 1 of 2

help with playlist creator

Posted: 21 Apr 2012 12:08
by mrwonker
Hi I have a batch script that creates a .m3u playlist file, it creates the playlist file from the content of the folder it is run from and also all subfolders from that location, I would really like to modify it so that instead of creating a single playlist file, it would create a seperate playlist file for each of the subfolders in the directory it is run from and save those files in the said subfolder. basically I would like to emulate what I could achieve by moving this batch file to each of the subfolders and running from there.

here is the script I am using at the minute.

Code: Select all

@ECHO OFF
SET currentfolder="%cd%"
CD ..
SET upperfolder="%cd%"
IF %upperfolder:~-2,-1%==\ SET upperfolder=%upperfolder:\=%
CALL SET folder=%%currentfolder:%upperfolder:"=%\=%%
CD %folder%
FOR /F "tokens=*" %%A IN ('dir /s /b *.asf,*.avi,*.divx,*.flv,*.m2ts,*.mp4,*.mpg,*.ogm,*.wmv,*.mp3,*.wma,*.mkv,*.flac, *TS.ifo') DO CALL :loop "%%~A"
GOTO end

:loop
SET file="%~1"
CALL SET file2="%%file:*%currentfolder:"=%\=%%"
SET file2=%file2:~0,-1%
setlocal enabledelayedexpansion
>>"%folder:"=%.m3u" ECHO !file2:"=!
GOTO :EOF

:end


would really appreciate any help given, I have done a bit of script writing with autohotkey but I think batch files would be better for what I want to achieve but I find the coding much more complicated.

thanks, James.

Re: help with playlist creator

Posted: 21 Apr 2012 12:54
by Fawers
Um, I understood what you want - and I am working on it already -, but I didn't really understand your code - which means I'm not working based on it.

I tested it a couple of times, and %folder% is always equal to %currentfolder%.
Besides, you could have used PUSHD .. instead of SET currentfolder="%cd%", CD .. , etc.

Tell me, you want your playlists separated by extension? Or it's okay to have all of them in one file?

Edit: one more question, what do you want %folder% to be? Only the folder name (no full path)?

Re: help with playlist creator

Posted: 21 Apr 2012 13:04
by mrwonker
thanks for your response and trying to help, to give an example of what I want it to do, say I have a folder called 'parent' in that folder I have a number of other folders 'child 1', 'child 2' etc in these 'child' folders each has another folder 'grandchild' its within these grandchild folders where the files are that the playlist is to be made from. I would like to run the script from the 'parent folder' and I would like a playlist file to be created in each of the 'child' folders relating to whats in each of their 'grandchild' folders.

hope that makes sense, thanks again.

Re: help with playlist creator

Posted: 21 Apr 2012 13:12
by Fawers
I see. I'm working on it.

1st Edit: Here's the first method (if you don't mind having all file types in the same file).
(Untested)

Code: Select all

@echo off
setlocal
:START
set "playlist=playlist.m3u"
for /f "delims=" %%a in ('dir /b /ad') do (
  pushd %%a
  for /f "delims=" %%b in ('dir /b /ad') do (
    pushd %%b
    >>"%%~fb.\%playlist%" dir /b /on *.asf *.avi *.divx *.flv *.m2ts *.mp4 *.mpg *.ogm *.wmv *.mp3 *.wma *.mkv *.flac *TS.ifo
    popd
  )
  popd
)

:EOF
endlocal

2nd Edit: method #2 (each file type in a different m3u playlist)
Once again, untested:

Code: Select all

@echo off
setlocal
:START
::set "playlist=playlist.m3u"
::playlist variable is not needed in 2nd method
for /f "delims=" %%a in ('dir /b /ad') do (
  pushd %%a
  for /f "delims=" %%b in ('dir /b /ad') do (
    pushd %%b
    for %%f in (.asf .avi .divx .flv .m2ts .mp4 .mpg .ogm .wmv .mp3 .wma .mkv .flac TS.ifo) do (
      if exist *%%f (>>"%%~Fb.\playlist%%f.m3u" dir /b /on *%%f)
    )
    popd
  )
  popd
)

:EOF
endlocal

Re: help with playlist creator

Posted: 21 Apr 2012 13:18
by mrwonker
:D

Re: help with playlist creator

Posted: 21 Apr 2012 13:31
by Fawers
I've posted 2 codes. They are untested, so I'd like you to tell me whether they work or not.

Hope it will do the job. :wink:

Re: help with playlist creator

Posted: 21 Apr 2012 14:40
by mrwonker
thanks tested both scripts, the 2nd script doesn't seem to produce a playlist anywhere that I can find, the first one produces the playlist and saves to the 'grandchild' folder where the files are, Is it possible for it to save in the folder a level up, to the 'child' folder instead? obviously for the playlist to work from this folder it would need the additional path to the 'grandchild' folder added at start of each line in the playlist file. I hadn't thought about having a different playlist for different file types its certainly not what is required at this time, though its an interesting concept and would have been another script to add to the collection had it worked but my main thing would be just to get your first script working with the tweak as described.
thanks.

Re: help with playlist creator

Posted: 21 Apr 2012 15:25
by Fawers
You're right. The 2nd script won't output anything to any file. I'm trying to figure how to make it output to a file out (since I liked the idea).

And, yes, there is a way. It is recommended, however, that you delete all the .M3U files created before, because I use the ">>" redirection method (which will not create a new file if a file with the same name exists, but will append the new output to it). Deleting previous .m3u playlists will avoid media duplicates in them.
To batch delete m3u files, run the DEL command from the command line (assuming you've got it pointing to the 'parent' folder):

Code: Select all

del /s /q *.m3u

You can ommit the /q switch to have a prompt asking you whether you want to delete each file or not.

The edited code (with moving from 'grandchild' folder to 'child' folder feature):

Code: Select all

@echo off
setlocal
title %~n0
:START
echo Creating playlists...
2>nul (
for /f "delims=" %%a in ('dir /b /ad') do (
  pushd "%%a"
  for /f "delims=" %%b in ('dir /b /ad') do (
    pushd "%%b"
    for /f "delims=" %%p in ('dir /b /on *.asf *.avi *.divx *.flv *.m2ts *.mp4 *.mpg *.ogm *.wmv *.mp3 *.wma *.mkv *.flac *TS.ifo') do (
      (echo %%~Fp)>>"%%~nb.m3u"
    )
    move "%%~nb.m3u" ..
    popd
  )
  popd
)
)

:EOF
endlocal

Re: help with playlist creator

Posted: 21 Apr 2012 16:13
by foxidrive
This should do what is described in the first post, but it's untested. The filename is currently "directory name.m3u" in each folder.
All the m3u file contains is the list of filenames, right?

@echo off
for /f "delims=" %%a in ('dir /ad /b /s') do (
pushd "%%a"
dir /b /a-d *.asf,*.avi,*.divx,*.flv,*.m2ts,*.mp4,*.mpg,*.ogm,*.wmv,*.mp3,*.wma,*.mkv,*.flac, *TS.ifo >"%%~na.m3u"
popd
)

Re: help with playlist creator

Posted: 21 Apr 2012 16:16
by Fawers
Actually, not only is it tested, but also I began using it. It's a very interesting idea that he's had.

Re: help with playlist creator

Posted: 21 Apr 2012 16:25
by mrwonker
ok nearly there the last script creates a playlist file in the grandchild folder and the child folder though the child folder playlist is blank persumably because it is creating on from the files in that folder which there arent.

the 2nd to last script is nearly exactly what I want the only thing is which my fault I didn't specify but the is it possible for the paths to the files in the playlist be more local and not the fullpath eg
child 1\playlistitem1
child 1\playlistitem2

rather than C:\child 1 playlistitem1
rather than C:\child 2 playlistitem1

reason being is the folders will likely get moved around from time to time but the contents remain the same so this way it would make them portable for me.

thanks.

Re: help with playlist creator

Posted: 21 Apr 2012 16:28
by mrwonker
sorry hadn't realised the last post was by another user, if my reply didnt quite make sense, glad its getting some interest, only come accross this site today seems like a good comunity on here. :D

Re: help with playlist creator

Posted: 21 Apr 2012 17:22
by mrwonker
also noticed that last script you offered Fawers, didn't append the playlist but rather created a new one correct with whatever is in the folder which is great as no clean up required if I want to do a batch refresh after changes. :D so just that removing of the unneeded extra file path and it will be absolutely amazing.

Re: help with playlist creator

Posted: 21 Apr 2012 22:13
by Fawers
Ok. We can do something inside the FOR loops. Do you need it in a hurry?

Re: help with playlist creator

Posted: 21 Apr 2012 22:24
by mrwonker
think you may have misunderstood my last post, It works great was saying that you mentioned earlier that it would append the playlists which wouldn't have been ideal, but it doesn't it overwrites the playlist with exactly what it should be so i can make whatever changes I like removing/adding files run the script and all my playlists will be correct no need to delete the playlists first, so really happy about that, no my only issue now to make the 'parent folders portable' I'd just like the paths in the playlist file to be as described earlier so.

child 1\playlistitem1
child 1\playlistitem2

instead of

C:\child 1 playlistitem1
C:\child 1 playlistitem2