Help - Search - Members - Calendar
Full Version: Batch m3u playlist creation for all folders
Hydrogenaudio Forums > Hydrogenaudio Forum > General Audio
zambaretzu
I've been trying to find an easy way to do this, but I can't seem to find exactly what I need. I just want a simple way to:
  • go through all my folders recursively
  • for each folder with mp3s and oggs, making an m3u for each
  • have the m3u in the actual folder, not the root folder
  • name the m3u just like its containing folder.

Any help is appreciated.
masterofimages
QUOTE (zambaretzu @ Jul 3 2006, 19:54) *
I've been trying to find an easy way to do this, but I can't seem to find exactly what I need. I just want a simple way to:
  • go through all my folders recursively
  • for each folder with mp3s and oggs, making an m3u for each
  • have the m3u in the actual folder, not the root folder
  • name the m3u just like its containing folder.
Any help is appreciated.


This script should sort you out:

CODE
Const ForReading = 1, ForWriting = 2, ForAppending = 8

delete = false
set args = WScript.Arguments
if args.Count > 0 then
    if LCase(args(0)) = "-d" then
        delete = true
    end if
end if

set fso = createobject("scripting.filesystemobject")
wscript.echo WriteM3u(fso.GetAbsolutePathName("."), delete) & " files written"

function WriteM3u(path, delete)
    dim count
    set fso = createobject("scripting.filesystemobject")
    set fdr = fso.GetFolder(path)
    if fdr.SubFolders.Count = 0 then
        m3u = path & "\" & fdr.name & ".m3u"
        if fso.FileExists(m3u) then
            if delete then
                wscript.echo "... deleting existing file"
                fso.DeleteFile m3u
            else
                wscript.echo "... renaming existing file"
                fso.MoveFile m3u, m3u & ".old"
            end if
        end if
        wscript.echo "... writing """ & fdr.name & ".m3u"""
        set m3ufile = fso.OpenTextFile(m3u, ForWriting, True)
        for each f in fdr.Files
            if right(f.Name, 3) = "mp3" or right(f.Name, 3) = "ogg" then
               m3ufile.WriteLine(f.Name)
            end if
        next
        m3ufile.Close
        count = 1
    else
        count = 0
        for each subfolder in fdr.subfolders
            wscript.echo "Searching """ & subfolder.path & """"
            count = count + WriteM3u(subfolder.path, delete)
        next
    end if
    
    WriteM3u = count
end function


Just save it to "WriteM3u.vbs" and stick it in the root directory of your mp3 and ogg files. To run, type:

CODE
cscript WriteM3u.vbs


By default it will rename any existing m3u files it finds. If you would rather delete them, just add "-d" to the end of the command-line, i.e.

CODE
cscript WriteM3u.vbs -d


-Paul
carmik
TAG can also do this from the command line. I use Speek's tag frontend, as the number of switches can be quite intimidating.
Julien
Thank you Paul, this is a nice and useful script
masterofimages
QUOTE (Julien @ Jul 4 2006, 01:14) *
Thank you Paul, this is a nice and useful script


Glad you found it useful. Enjoy biggrin.gif
zambaretzu
Yeah, thanks, it's awesome. If you wrote it yourself, maybe you could help me make one modification to it? It doesn't work right for albums with multiple CDs, because my folder structure for them looks like this:
Foldername\Disc 1\
Foldername\Disc 2\

Right now the script writes:
Foldername\Disc 1\Disc 1.m3u
Foldername\Disc 2\Disc 2.m3u

Is there any way to make it write (ideal):
Foldername\Disc 1\Foldername (Disc 1).m3u
Foldername\Disc 2\Foldername (Disc 2).m3u

or:
Foldername\Foldername (Disc 1).m3u
Foldername\Foldername (Disc 2).m3u

or even:
Foldername\Foldername.m3u (with files from both folders)

This would make the script supremely useful for me, hope you can do it smile.gif
masterofimages
QUOTE (zambaretzu @ Jul 5 2006, 22:51) *
Yeah, thanks, it's awesome. If you wrote it yourself, maybe you could help me make one modification to it? It doesn't work right for albums with multiple CDs, because my folder structure for them looks like this:
Foldername\Disc 1\
Foldername\Disc 2\
...


No problem zambaretzu. Try this:

CODE
Const ForReading = 1, ForWriting = 2, ForAppending = 8

delete = false
set args = WScript.Arguments
if args.Count > 0 then
    if LCase(args(0)) = "-d" then
        delete = true
    end if
end if

set fso = createobject("scripting.filesystemobject")
wscript.echo WriteM3u(fso.GetAbsolutePathName("."), delete) & " files written"

function WriteM3u(path, delete)
    dim count
    set fso = createobject("scripting.filesystemobject")
    set fdr = fso.GetFolder(path)
    if fdr.SubFolders.Count = 0 then
        if len(fdr.Name) = 6 and left(fdr.Name, 5) = "Disc " then
            m3uName = fdr.ParentFolder.Name & " (" & fdr.Name & ").m3u"
        else
            m3uName = fdr.Name & ".m3u"
        end if
        m3u = path & "\" & m3uName
        if fso.FileExists(m3u) then
            if delete then
                wscript.echo "... deleting existing file"
                fso.DeleteFile m3u
            else
                wscript.echo "... renaming existing file"
                fso.MoveFile m3u, m3u & ".old"
            end if
        end if
        wscript.echo "... writing """ & m3uName & """"
        set m3uFile = fso.OpenTextFile(m3u, ForWriting, True)
        for each f in fdr.Files
            if right(f.Name, 3) = "mp3" or right(f.Name, 3) = "ogg" then
               m3uFile.WriteLine(f.Name)
            end if
        next
        m3uFile.Close
        count = 1
    else
        count = 0
        for each subFolder in fdr.SubFolders
            wscript.echo "Searching """ & subFolder.Path & """"
            count = count + WriteM3u(subFolder.path, delete)
        next
    end if
    
    WriteM3u = count
end function


smile.gif
zambaretzu
Thanks a lot masterofimages.

There's still a small issue: for some albums, I have artwork, stored under \Foldername\art\. The script writes an empty \Foldername\art\art.m3u but no \Foldername\Foldername.m3u like it should.
masterofimages
QUOTE (zambaretzu @ Jul 6 2006, 20:49) *
Thanks a lot masterofimages.

There's still a small issue: for some albums, I have artwork, stored under \Foldername\art\. The script writes an empty \Foldername\art\art.m3u but no \Foldername\Foldername.m3u like it should.


So it does ohmy.gif And then there was... version 3 - with added comments for extra juicyness!

CODE
Const ForReading = 1, ForWriting = 2, ForAppending = 8

' Parse command-line arguments
delete = false
set args = WScript.Arguments
if args.Count > 0 then
    if LCase(args(0)) = "-d" then
        delete = true
    end if
end if

' Write m3u files for current directory tree
set fso = CreateObject("Scripting.FileSystemObject")
wscript.echo WriteM3u(fso.GetAbsolutePathName("."), delete) & " files written"

' Recursive function to write m3u files for a given path
function WriteM3u(path, delete)
    count = 0
    set fso = CreateObject("Scripting.FileSystemObject")
    set fdr = fso.GetFolder(path)
    
    ' Write m3u file for each subfolder
    if fdr.SubFolders.Count > 0 then
        for each subFolder in fdr.SubFolders
            count = count + WriteM3u(subFolder.path, delete)
        next
    end if
    
    ' If no files found in subfolders, write m3u file for this folder
    if count = 0 then
        wscript.echo "Scanning """ & fdr.Path & """"
        ' Build list of mp3/ogg files
        mp3List = ""
        for each f in fdr.Files
            if right(f.Name, 3) = "mp3" or right(f.Name, 3) = "ogg" then
               mp3List = mp3List & f.Name & VBCrLf
            end if
        next
        
        ' If any files found, write m3u file
        if mp3List <> "" then
            ' Multi-disc folder handling
            if len(fdr.Name) = 6 and left(fdr.Name, 5) = "Disc " then
                m3uName = fdr.ParentFolder.Name & " (" & fdr.Name & ").m3u"
            else
                m3uName = fdr.Name & ".m3u"
            end if
            
            ' Existing m3u file handling
            m3u = path & "\" & m3uName
            if fso.FileExists(m3u) then
                if delete then
                    wscript.echo "... deleting existing file"
                    fso.DeleteFile m3u
                else
                    wscript.echo "... renaming existing file"
                    fso.MoveFile m3u, m3u & ".old"
                end if
            end if
            
            ' Write new m3u file
            wscript.echo "... writing """ & m3uName & """"
            set m3uFile = fso.OpenTextFile(m3u, ForWriting, True)
            m3uFile.Write(mp3List)
            m3uFile.Close
            count = 1
        else
            wscript.echo "... no mp3/ogg files found"
        end if
    end if
    
    ' Return m3u file count
    WriteM3u = count
end function
zambaretzu
Thank you, thank you! smile.gif
Tbenhov
Thanks for the great script!! Is it possible to make it name the playlists Artist - Album.m3u using the name of the album's containing folder?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.