################################################################################
# #
# ############################################################################ #
# # Title : CRC & Number Generating Namer (lol?) # #
# # Description : A quick hack for generating a file name with a CRC and # #
# # number in it based on some simple input. # #
# # Coded by : Justin "HEYO--!" Lee # #
# # Version : 1.0 # #
# # Date : 2008-11-23 # #
# # License : GNU General Public License v.2 # #
# # Copyright (C) 2008 Justin Lee <kool.name at gmail.com> # #
# ############################################################################ #
# #
# ############################################################################ #
# # This program is free software; you can redistribute it and/or # #
# # modify it under the terms of the GNU General Public License version 2 # #
# # as published by the Free Software Foundation. # #
# # # #
# # This program is distributed in the hope that it will be useful, # #
# # but WITHOUT ANY WARRANTY; without even the implied warranty of # #
# # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # #
# # GNU General Public License for more details. # #
# # # #
# # You should have received a copy of the GNU General Public License # #
# # along with this program; if not, write to the Free Software # #
# # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # #
# # 02110-1301, USA. Or, see: <http://www.gnu.org/licenses/> # #
# ############################################################################ #
# #
################################################################################
use strict;
use String::CRC32;
use Getopt::Std;
my %opt;
getopts('d:s:e:r:E', \%opt) or die "Unable to parse opts: $!\n";
unless (defined $opt{'d'} and defined $opt{'s'} and ($opt{'E'} or defined($opt{'e'}))) {
print "Usage: @{[ basename($0) ]} -d < /path/to a/directory >" .
" -s \"Title String\" -e \"ext\"\n" .
"Use -r < regex > for a custom number finding regex,\n" .
"default is '[^v](\d+)`.\nUse -E instead of -e \"ext\"" .
"if you'd like to auto-find extensions.";
exit 1;
}
my $gex = defined $opt{'r'} ? $opt{'r'} : '[^v](\d+)';
opendir(THERE, $opt{'d'}) or die "Unable to open working directory: $!\n";
my @files = readdir(THERE);
closedir(THERE);
chdir $opt{'d'} or die "Unable to enter working directory: $!\n";
foreach my $file (sort @files) {
next if -d $file;
next if $file =~ /^\.\.?$/o;
my ($num,$ext);
$file =~ /$gex/oi;
$num = $1;
if ($opt{'E'}) {
$file =~ /(\w+)$/oi;
$ext = $1;
}
open(CRCFILE,$file) or warn("Unable to open file: $!"), next;
binmode(CRCFILE);
my $nfile = sprintf(
"%s - %02d [\U%x\E].%s",
$opt{'s'},
$num,
crc32(*CRCFILE),
$opt{'E'} ? $ext : $opt{'e'}
);
close(CRCFILE);
rename($file,$nfile) or die "Unable to rename: $!\n";
print $nfile, "\n";
}