I finally got around to writing a script to rename all my music files to the same convention. That being either <track>-<song>.mp3 or <song>.mp3 with no spaces (I prefer underscores instead) and all lowercase. I know, it looks so simple! But the problem is all those occasions where you have something like <track>_-_<SONG>_-_(<remix by>).mp3 which just looks horrible! What this script does could be done in a lot less lines than I have here, but I thought it might be a good way for someone who is unsure of regular expressions to get a grasp on the topic.
So heres the script:
#!/usr/bin/perl -w
use strict;
# Open 'find' process to list files recursively with paths
open(FIND, "find |");
while(<FIND>) {
# remove leading / trailing whitespace
chomp;
# Don't rename ourself
next if $_ eq $0;
# create temp file (windows wont allow to rename in place from uppercase to lowercase)
my $name = $_;
my $tmp = $_.'~';
rename($name, $tmp);
# make lowercase
$name = lc($name);
rename($tmp, $name);
my $newname = $name;
# remove apostrophes
$newname =~ s/[']//g;
# remove round brackets and replace with hyphens
$newname =~ s/[()]/-/g;
# remove spaces and replace with underscores
$newname =~ s/ /_/g;
# remove where in sequence there is underscore, hyphen, underscore and replace with a hyphen
$newname =~ s/_-/-/g;
$newname =~ s/-_/-/g;
# where there are one or more digits followed by an underscore change the underscore to a hyphen
$newname =~ s/(d+)_/$1-/g;
# remove all ampersands and replace them with '_and_'
$newname =~ s/&/_and_/g;
# remove underscores where there are 2 or more, and replace with a single underscore
$newname =~ s/_{2,}/_/g;
# write out the changes
rename($name,$newname);
}
close(FIND);
This is just a handy script I use for renaming my music collection. It got to be too much trouble having the first letter of each word capitalised, getting new music with the names all in caps, or all in lowercase…so I decided to make everything lowercase and write a script to do it for me!
#!/usr/bin/perl -w
use strict;
# open 'find' process to list files recursively with paths
open(FIND, "find |");
while(<FIND>) {
chomp;
# don't rename ourself if script in same as executing
next if $_ eq $0;
# first move the file to $name~ and then back to the lowercase original to allow for fat32 ignoring case,
# and therefore claiming that a file with this name already exists
my $name = $_;
my $tmp = $_.'~';
rename($name, $tmp);
rename($tmp, lc($name));
}
close(FIND);
To start off the script just opens a handle on the linux find command with an output pipe and calls it…well…FIND. The while loop will then iterate through each line the find command returns and rename using the rename() function.
The rename function takes in two arguments, the old name (as returned by find) and the new name. As the current returned value we are looking at is stored in the special variable $_, we can pass this as the old file name. We can then just use the lc() function to convert the old mane to lowercase by putting $_ as the argument for lc(). Finally we close the filehandle on FIND. And voila!