http://www.perlmonks.org?node_id=135318
Category: Audio Related Programs
Author/Contact Info Gavin Estey, gavin@estey.com
Description: 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;
    }
    
}