Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-02
12
Medium Priority
?
3,719 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 29

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 29

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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 29

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 29

Accepted Solution

by:
Michael Pfister earned 1000 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 29

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 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