#!/usr/bin/perl # batch processor for removing track numbers from filenames of mp3s and making # m3u files out of them #$Id: denumerate 143 2005-09-12 16:08:08Z steve $ #$Log$ #Revision 1.4 2005/09/12 16:08:08 steve #slightly better regex # #Revision 1.3 2003/03/05 07:27:03 steve #deals with a . after the number # #Revision 1.2 2003/01/25 23:11:41 steve #adds comments where missing files should be # #Revision 1.1 2002/12/13 21:07:21 steve #Initial revision # ############################### use strict; #cracks the whip use Getopt::Std; getopts("hm:"); our( $opt_h, $opt_m ); my @files = @ARGV; usage() if ($opt_h || !@files); # the default m3u filename is the current directory's name with .m3u added my ($def_m3u) = $ENV{'PWD'} =~ /.+\/(.+?)$/; $def_m3u .= ".m3u"; my $m3u_file = $def_m3u; $m3u_file = $opt_m if defined $opt_m; my @playlist = (); foreach my $file (sort @files){ # try to pull out the track number if it's there if(my ( $tracknum, $filename ) = $file =~ /^\s*(\d{1,2})\.?\s*(?:\-{1,8}\s*)?(.+)$/){ my $new_filename = $file; if( rename $file, $filename ){ print "renamed \"$file\" to \"$filename\"\n"; $new_filename = $filename; }else{ warn "could not rename \"$file\" to \"$filename\": $!\n"; } $playlist[$tracknum] = $new_filename; }else{ warn "cannot extract a track number from $file\n"; } } if( @playlist ){ print "Writing track list to $m3u_file\n"; open M3U, ">$m3u_file" or die "cannot open m3u file (\"$m3u_file\") ". "for writing: $!\n"; for(my $i = 1; $i < @playlist; $i++){ if( $playlist[$i] ){ print M3U $playlist[$i], "\n"; }else{ print M3U "# track $i goes here\n"; } } close M3U; } sub usage{ die< USAGE }