I was fed up of not having a playlist in each directory of mp3s, mainly as I'm lazy and just want to click on the .m3u file :) I also had a stack of playlists that had full paths which broke things when I moved stuff around.
This script with create a playlist in each directory that contains mp3s, nothing else exciting.
use strict;
use warnings;
use File::Basename;
my $del_m3u = 1;
my $dir = $ARGV[0] || die "You need to provide a directory";
handle_dir($dir);
exit;
sub handle_dir {
my $dir = shift;
my @songs = ();
print "Dir: $dir\n";
opendir DIR, $dir or die "Unable to open dir -- $!";
foreach my $file (sort readdir DIR) {
if (-d "$dir/$file") {
next if $file =~ /^\.+$/;
handle_dir("$dir/$file")
} elsif ($file =~ /\.[Mm]3[Uu]$/ && $del_m3u) {
unlink "$dir/$file" or die "Unable to delete $file -- $!";
} elsif ($file =~ /\.[Mm][Pp]3$/) {
push @songs, $file;
}
}
closedir DIR;
my $playlist = $dir . "/" . basename($dir) . ".m3u";
if (@songs > 0) {
open OUT, ">$playlist" or die "Unable to write to $playlist --
+ $!";
print OUT $_, "\n" foreach @songs;
close OUT;
}
}
Doh, sorry for the double post. Not sure what happened.
I just had a thought, what about making the playlists for a directory also contain all the mp3s? So that if you had c:/mp3s/cd/1 and c:/mp3s/cd/2 you'd end up with a m3u in c:/mp3s/cd that had both dirs in it's playlist.
Just add a:
return @songs;
and change
handle_dir("$dir/$file");
to
unshift @songs, handle_dir("$dir/$file");