CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions: Re: [jwhit61] Track. Title format for audio files: Edit Log



KevinR
Veteran


Dec 23, 2008, 10:18 AM


Views: 7406
Re: [jwhit61] Track. Title format for audio files

assuming the names always start with two digits here is some code I came up with. There are also many modules listed on CPAN you may want to look at.


Code
my $file = '01-rolling      stones(paint_it_black).mp3';  
my $new = FixFileName($file);
print $new;

sub FixFileName {
my $file = $_[0] or return "No filename entered. Please enter a filename.";
my $oldfile = $file;
my ($track, $name, $ext);
if ($file =~ /^(\d\d)(.*)(\.\w+)$/) {
($track, $name, $ext) = ($1, $2, $3);
}
else {
print "Format not recognized\n";
exit(0);
}
#replace underscores with a space
$name =~ tr/_/ /;
#collapse multiple spaces
$name =~ tr/ / /s;
#change first alpha to uppercase using the \b anchor as the word boundary
$name =~ s/\b([a-z]+)\b/ucfirst(lc($1))/eg;
unless ($oldfile eq "$track. $name$ext") {
rename ( $oldfile, "$track. $name$ext" ) or die "Can't rename $oldfile to $newfile : $!";
}
return ("$track. $name$ext");
}

-------------------------------------------------


(This post was edited by KevinR on Dec 23, 2008, 7:48 PM)


Edit Log:
Post edited by KevinR (Veteran) on Dec 23, 2008, 10:24 AM
Post edited by KevinR (Veteran) on Dec 23, 2008, 10:24 AM
Post edited by KevinR (Veteran) on Dec 23, 2008, 10:25 AM
Post edited by KevinR (Veteran) on Dec 23, 2008, 1:40 PM
Post edited by KevinR (Veteran) on Dec 23, 2008, 1:51 PM
Post edited by KevinR (Veteran) on Dec 23, 2008, 7:48 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives