I have a collection of Bleach anime series. Filename was in format with prefix "Bleach - xxx.avi". I wanted to remove the prefix and try to do it manually, but doing so with 200 over files is kind of annoying. I knew there's a way to rename file extension from uppercase to lowercase or even adding prefix easily with just one single line of command. But I'm not sure whether it's possible to remove the prefix (if there's a linux command guru out there knows how to do it, kindly show it to me).
Since I'm crazy learning Perl scripting lately (thanks to my latest assignment), I've created a script for my problem. My first programming in Linux!!!
#!/usr/bin/perl
use strict;
my $directory = @ARGV[0];
opendir(DIRECTORYHANDLER, $directory) || die("Cannot open directory");
my @thecontents = readdir(DIRECTORYHANDLER);
closedir(DIRECTORYHANDLER);
my $counter = 1;
foreach my $content (@thecontents)
{
if($content =~ /Bleach\s-\s\d+.avi/i)
{
my $fullpath = "$directory/$content";
print "$fullpath\n";
my $newname = sprintf("%03d.avi", $counter);
my $newpath = "$directory/$newname";
rename($fullpath, $newpath);
$counter++;
}
}
To be honest, once I knew how simple it is to read a file and passing it to a variable in Perl script, I've started to love it. My understanding on regular expression is getting better and thanks to Ady for encouraging me to use Perl instead of .NET console app for the assignment.
No comments:
Post a Comment