help with playlist creator

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
mrwonker
Posts: 11
Joined: 21 Apr 2012 11:56

help with playlist creator

#1 Post by mrwonker » 21 Apr 2012 12:08

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.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: help with playlist creator

#2 Post by Fawers » 21 Apr 2012 12:54

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

mrwonker
Posts: 11
Joined: 21 Apr 2012 11:56

Re: help with playlist creator

#3 Post by mrwonker » 21 Apr 2012 13:04

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.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: help with playlist creator

#4 Post by Fawers » 21 Apr 2012 13:12

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
Last edited by Fawers on 21 Apr 2012 14:12, edited 4 times in total.

mrwonker
Posts: 11
Joined: 21 Apr 2012 11:56

Re: help with playlist creator

#5 Post by mrwonker » 21 Apr 2012 13:18

:D

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: help with playlist creator

#6 Post by Fawers » 21 Apr 2012 13:31

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:

mrwonker
Posts: 11
Joined: 21 Apr 2012 11:56

Re: help with playlist creator

#7 Post by mrwonker » 21 Apr 2012 14:40

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.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: help with playlist creator

#8 Post by Fawers » 21 Apr 2012 15:25

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: help with playlist creator

#9 Post by foxidrive » 21 Apr 2012 16:13

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
)

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: help with playlist creator

#10 Post by Fawers » 21 Apr 2012 16:16

Actually, not only is it tested, but also I began using it. It's a very interesting idea that he's had.

mrwonker
Posts: 11
Joined: 21 Apr 2012 11:56

Re: help with playlist creator

#11 Post by mrwonker » 21 Apr 2012 16:25

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.

mrwonker
Posts: 11
Joined: 21 Apr 2012 11:56

Re: help with playlist creator

#12 Post by mrwonker » 21 Apr 2012 16:28

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

mrwonker
Posts: 11
Joined: 21 Apr 2012 11:56

Re: help with playlist creator

#13 Post by mrwonker » 21 Apr 2012 17:22

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.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: help with playlist creator

#14 Post by Fawers » 21 Apr 2012 22:13

Ok. We can do something inside the FOR loops. Do you need it in a hurry?

mrwonker
Posts: 11
Joined: 21 Apr 2012 11:56

Re: help with playlist creator

#15 Post by mrwonker » 21 Apr 2012 22:24

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

Post Reply