Solved

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

Posted on 2008-10-02
12
3,181 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now