Link to home
Start Free TrialLog in
Avatar of brunces
brunces

asked on

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

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
Avatar of Michael Pfister
Michael Pfister
Flag of Germany image


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"
-------------
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"

Avatar of brunces
brunces

ASKER

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

Avatar of brunces

ASKER

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
Avatar of brunces

ASKER

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
Avatar of brunces

ASKER

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
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.

Avatar of brunces

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Michael Pfister
Michael Pfister
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brunces

ASKER

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
Avatar of brunces

ASKER

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

brunces