#!/usr/bin/perl -w use strict; use Getopt::Long; my $i; my $j; my $dir; my $file; my $nodots=0; my $norename=0; my $fixtracks=0; my $splitcase=0; my $spacedash=0; my $swap=0; sub usage { die "$0 [--fixtracks] [--norename] [--nodots] [--splitcase] [--spacedash] [--swap=1/2/3/4]\n"; } GetOptions("fixtracks" => \$fixtracks, "norename" => \$norename, "nodots" => \$nodots, "splitcase" => \$splitcase, "spacedash" => \$spacedash, "swap=i" => \$swap) or usage; open (INVERT, ">/tmp/revertrename") or die "Can't open /tmp/revertrename: $!"; while ($i = shift @ARGV) { $i =~ s/"/\\"/g; $i =~ m#^(.*?)/?([^/]+)$#; ($dir,$file) = ($1, $2); $dir.="/" if $dir; $file =~ s/_/ /g; $file =~ s/\./ /g if $nodots; $file =~ s/([a-z])([^a-z ])/$1 $2/g if ($splitcase); # This is broken by the above :) $file =~ s/mp 3$/mp3/; $file =~ s/MP3$/mp3/; $file =~ s/ mp3$/.mp3/; $file =~ s/ *.mp3$/.mp3/; $file =~ s/mp3\s+$/.mp3/; $file =~ s#^\[([^-\]]+)\]#$1 #; $file =~ s#^\(([^-)]+)\)#$1 #; # Fix broken windows characters $file =~ s/‚/é/g; $file =~ s/Š/é/g; $file =~ s/…/à/g; $file =~ s/ˆ/ê/g; $file =~ s/—/ù/g; if ($spacedash) { $file =~ s#([^ ])-#$1 -#g; $file =~ s#-([^ ])#- \u$1#g; } if ($swap == 1) { $file =~ s#^(.)([^-]*[^ ]) *- *(.)([^-]*)(.*)$#\u$3$4- \u$1$2 $5#; } elsif ($swap == 2) # swap name and author with no dash { $file =~ s#^(.)([^-]*[^ ]) *- *(.)(.*[^ ]) *- *(.)([^-]*).mp3$#\u$1$2 - \u$5$6 - $3$4.mp3#; } elsif ($swap == 3) # swap name and author with a dash { $file =~ s#^(.)([^-]*[^ ]) *- *(.)([^-]*[^ ]) *- *(.)(.*).mp3$#\u$1$2 - \u$5$6 - $3$4.mp3#; } elsif ($swap == 4) # swap name and author with two dashes and track number { $file =~ s#^(.)([^-]*[^ ]) *- *(.)([^-]*[^ ]) *- (\d\d) *(.)(.*) *- *(.)([^-]*[^ ]*).mp3$#\u$1$2 - \u$3$4 - $5 \u$8$9 - $6$7.mp3#; } else { $file =~ s#^(.)([^-]*[^ ]) *- *(.)([^-]*)(.*)$#\u$1$2 - \u$3$4$5#; } $file =~ s#^(.*[^ ] - )(.)(.*[^ ]) *- *(.)([^-]*)(.*)$#$1\u$2$3 - \u$4$5$6#; $file =~ s#\(([^-]+)\).mp3$#- $1.mp3#; $file =~ s#\[([^-]+)\].mp3$#- $1.mp3#; $file =~ s#'([^-]+)'.mp3$#- $1.mp3#; $file =~ s# (\d\d) - # - $1 #; if ($fixtracks) { $file =~ s# (\d\d) - # - $1 #; $file =~ s# - (\d\d) (\S)# - $1 $2#; } $file =~ s# - (\d\d) (.)# - $1 \u$2#; $file =~ s/mp3\s+$/mp3/; $file =~ s/mp3 /mp3_/; $file =~ s/- -/-/g; $file =~ s/\) \(/ - /g; $file =~ s/\s+$//; $j="$dir$file"; if ($i ne $j) { print "Going from $i \nto $j\n"; rename($i,$j) unless $norename; print INVERT "/bin/mv \"$j\" \"$i\"\n"; } else { print "$i will be left unchanged\n"; } }