Solved

How to create a playlist (m3u) via DOS (batch file)

Posted on 2008-10-02
12
3,383 Views
Last Modified: 2012-05-05
Friends,

I'm trying to create a batch file which will generate a playlist by right-clicking on a folder and choosing a specific option from the context menu.

Here is the batch code:

-------------------------------
@echo off
cd %1
echo %~f1
dir /o:n/a/b/s *.mp3 *.wma > "D:\John\Playlists\%~nx1.m3u"
-------------------------------

The point is: It lists the files in the folder and save that list as a m3u file with that folder name. That's it.

It wasn't me who created this code, so I don't understand it so much. I just get the reasoning. It's working perfectly, but there are some issues I'd like to correct. So, my questions are:

1) When the folder name has commas (Ex.: Earth, Wind and Fire), the code gets only the first word and saves the m3u file with that name (Ex.: Earth.m3u). How can I avoid this?

2) If the folder or file names have accentuation (I'm Brazilian, so my native language is Portuguese), it gets a real mess because all accentuated letters turn into strange characters. How can I avoid this?

3) As you can see, the DIR command filters only *.mp3 and *.wma files, so is there a way to avoid the creation of the m3u file if the clicked folder has none of both file types in it? Using that ERRORLEVEL, maybe, I don't know.

Well, that's it. I hope you can help me. Thank you very much for your attention.

brunces
0
Comment
Question by:brunces
  • 7
  • 5
12 Comments
 
LVL 28

Expert Comment

by:Michael Pfister
ID: 22624301

2) Nothing you can do about except avoiding accentuated letters

3) Change batch to
-------------
@echo off
cd %1
If Exists *.wma Goto :M3U
If Exists *.mp3 Goto :M3U
Goto :EOF
:M3U
echo %~f1
dir /o:n/a/b/s *.mp3 *.wma > "D:\John\Playlists\%~nx1.m3u"
-------------
0
 
LVL 28

Expert Comment

by:Michael Pfister
ID: 22624382
1)
I assume you have an additional entry in your registry under

HKEY_LOCAL_MACHINE/Software/Classes/Folder/Shell

something like Playlist (could be under HKEY_CURRENT_USER as well).

Under this key you find a key named command, containing a call to your playlist batch.

C:\Playlist\Playlist.cmd %1


Double-click the entry and add double-quotes around %1

C:\Playlist\Playlist.cmd "%1"

0
 

Author Comment

by:brunces
ID: 22625967
mpfister, thanks for your answer. I still have a couple of doubts...

1) I assume you have an additional entry in your registry under... C:\Playlist\Playlist.cmd "%1"

That's right! The point is that I was using "%L" instead of "%1". Now, with "%1", it worked fine. Thanks. :)

2) Nothing you can do about except avoiding accentuated letters

Gee, so it's impossible to do that?! Isn't there any ASCII parameter or whatever, I don't know, which would allow that to be done?

3) Change batch to
-------------
@echo off
cd %1
If Exists *.wma Goto :M3U
If Exists *.mp3 Goto :M3U
Goto :EOF
:M3U
echo %~f1
dir /o:n/a/b/s *.mp3 *.wma > "D:\John\Playlists\%~nx1.m3u"
-------------

Sorry, but it didn't work. :(

Also, after it has checked whether *.wma files exist or not, it goes immediately to :M3U, right? OK! But it doesn't come back to check *.mp3 files. Am I wrong? Those two checkings should be together, shouldn't they? Something like...

If Exists *.wma ...or... If Exists *.mp3 Goto :M3U (This is just an example. I don't know. It's just a guess.)

Hope you can still help me. Thank you. :)

brunces

0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

Author Comment

by:brunces
ID: 22626554
mpfister, help me understand this...

If Exists *.wma Goto :M3U
If Exists *.mp3 Goto :M3U

If wma files exist, it creates the list. If wma don't exist, it reads the next line and then checks if mp3 files exist. Is that it? If so, disregard my third comment above. Thanks. :)

brunces
0
 

Author Comment

by:brunces
ID: 22626632
mpfister,

I got it! It wasn't working because we must use IF EXIST and not IF EXISTS. Now it's working! :)

Now, the point is the accentuated letters only. If you figure out what to do, I will appreciate it. :)

brunces
0
 

Author Comment

by:brunces
ID: 22626852
mpfister,

I've found a problem. Sometimes, the folder I click on doesn't really have files. For example, I have a folder called Albums. In this folder I don't have any files, I have other folders with files, one for each album, and in "these" folders I have files. Then, the batch doesn't create the playlist because it checks the existance of files in folder Albums only.

That checking can't be based on existing files in the current folder only. It must check subfolders as well. Maybe it should be based on the DIR command, I don't know. Any suggestions? Thanks. :)

brunces
0
 
LVL 28

Expert Comment

by:Michael Pfister
ID: 22647783
Sorry for my typo...
Checking for existance in subdirs could look like this:


-------------
@echo off
cd %1
If Exist *.wma Goto :M3U
If Exist *.mp3 Goto :M3U
For /d %%A in (*) Do If Exist "%%A\*.wma" Goto :M3U
For /d %%A in (*) Do If Exist "%%A\*.mp3" Goto :M3U
Goto :EOF
:M3U
echo %~f1
dir /o:n/a/b/s *.mp3 *.wma > "D:\John\Playlists\%~nx1.m3u"
-------------

It will check for WMA and MP3 in the currenty directory and one directory level deep.

0
 

Author Comment

by:brunces
ID: 22652291
mpfister,

Thank you very much for your answer.

You said it's only one directory level deep. Isn't it possible to check all subfolders?

I've seen something related to this... for /f "token="... Is it useful?

Waiting for you. Thanks. :)

brunces
0
 
LVL 28

Accepted Solution

by:
Michael Pfister earned 250 total points
ID: 22657445
A dir can be slow. You can give this one a try. Dir sets an error level of 1 if the pattern isn't matched
-------------
@echo off
cd %1
If Exist *.wma Goto :M3U
If Exist *.mp3 Goto :M3U
dir *.wma /s
If Errorlevel 1 Goto :Check2
Goto :M3U
:Check2
dir *.mp3 /s
If Errorlevel 1 Goto :EOF
:M3U
echo %~f1
dir /o:n/a/b/s *.mp3 *.wma > "D:\John\Playlists\%~nx1.m3u"
-------------
0
 

Author Comment

by:brunces
ID: 22660327
mpfister, it's perfect! Thank you very much.

Just one last doubt... As I've told you, I run this batch clicking an item in the menu context which I have created in the registry. Is there a way to run it minimized? I thought of creating a shortcut and put the path to the shortcut in the registry, but maybe there's another way.

Thank you very much.

brunces
0
 
LVL 28

Expert Comment

by:Michael Pfister
ID: 22667503
0
 

Author Comment

by:brunces
ID: 22669913
Excellent, mpfister. You're the man! Thank you very much! :)

brunces
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question