Solved

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

Posted on 2008-10-02
12
3,477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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