2007/08/29

renameall utitlity

renameall.bat

@echo off

verify errors 2>nul
setlocal enableextensions
if errorlevel 1 echo unable to enable extensions

set USR_LOCAL_BIN=F:\usr\local\bin
perl %USR_LOCAL_BIN%\renameall2.pl %*

endlocal
echo on


renameall.pl

#!/bin/perl

#
#renameall.pl
#
# Syntax: renameall -uls ([*][0-9A-Za-z]+){1,3} ([*][0-9A-Za-z]+){1,3}
# Example: renameall *.* *.*

use strict;
use warnings;

#---------------------------------
# constant
my $TRUE = -1;
my $FALSE = 0;
my $SYNTAX = "Syntax: renameall ([*][0-9A-Za-z]+){1,3} ([*][0-9A-Za-z]+){1,3}";
my $OPT_UPPER = 1 << 2;
my $OPT_LOWER = 1 << 1;
my $OPT_SEMULATE = 1;

#---------------------------------
# parameter

#my $opt_semulate = $FALSE;
my $options = 0;
my $searchstr = '';
my $replacestr = '';
my @replacestrarr = {};
my $replacestrarrcnt = 0;

my $i;
my $j;
my $paramcnt;
for ($i = 0, $paramcnt = 0; $i < $#ARGV + 1; $i ++) {
#printf ("\$ARGV[$i]=$ARGV[$i]\n");
if (substr($ARGV[$i], 0, 1) eq '-') {
#printf ("option $ARGV[$i]\n");
for ($j = 1; $j < length($ARGV[$i]); $j ++) {
my $opt_char = substr($ARGV[$i], $j, 1);
if ($opt_char eq 's' || $ARGV[$i] eq '--semulate') {
#$opt_semulate = $TRUE;
$options |= $OPT_SEMULATE;
} elsif ($opt_char eq 'l' || $ARGV[$i] eq '--lower') {
$options |= $OPT_LOWER;
} elsif ($opt_char eq 'u' || $ARGV[$i] eq '--upper') {
$options |= $OPT_UPPER;
}
}
} else {
if ($paramcnt == 0) {
$searchstr = $ARGV[$i];
#printf ("\$searchstr = $ARGV[$i]\n");

$searchstr =~ s/([\.\[\]\(\)\/])/\\$1/g;
$searchstr =~ s/\*/(.*)/g;

#my @searcharr = split(/\*/,$searchstr);
#my $replacestrarrcnt = $#searcharr;# - 1;

} elsif ($paramcnt == 1) {
$replacestr = $ARGV[$i];
#printf ("\$replacestr = $ARGV[$i]\n");

#$replacestr =~ s/\./\\\./g;
#$replacestr =~ s/\*//;

@replacestrarr = split (/\*/, $replacestr);
$replacestrarrcnt = $#replacestrarr;
$replacestr =~ m/.$/;

if ($& eq '*') {
$replacestrarrcnt++;
}

$i = 1;

#while ($replacestr =~ m/\*/) {
# $replacestr =~ s/\*/\\$i/;
#}
}
$paramcnt ++;
}
}
printf ("\$options: %08.0b ---- -ULS\n", $options);

if ($paramcnt < 2) {
die "insufficient parameters.\n$SYNTAX\n";
}

# deal with the pattern that search arr > replace arr
for ($i = 0; $i < 10; $i ++) {
push (@replacestrarr, '');
}

#print (join ('*',@replacestrarr)."\n");
print ("internal pattern: \$searchstr=$searchstr;\$replacestr=$replacestr;\n");

#----------------------------------
# ls dir
opendir(THEDIR, ".");
my @selected=grep(/^$searchstr$/, readdir THEDIR);
closedir(THEDIR);
#print (join("\n",@selected)."\n");
if ($#selected == -1) {
die "nothing found.\n";
}

#----------------------------------
# rename the file one by one
foreach my $oldfilename (@selected) {
# convert old filename to new filename
my $newfilename = $oldfilename;
#print ($replacestrarrcnt);
if ($replacestrarrcnt == 0) {
$newfilename =~ s/$searchstr/$replacestrarr[0]/g;
} elsif ($replacestrarrcnt == 1) {
$newfilename =~ s/$searchstr/$replacestrarr[0]$1$replacestrarr[1]/g;
} elsif ($replacestrarrcnt == 2) {
$newfilename =~ s/$searchstr/$replacestrarr[0]$1$replacestrarr[1]$2$replacestrarr[2]/g;
} elsif ($replacestrarrcnt == 3) {
$newfilename =~ s/$searchstr/$replacestrarr[0]$1$replacestrarr[1]$2$replacestrarr[2]$3$replacestrarr[3]/g;
} else {
die "unsupported pattern $replacestr : too much '*' .\n";
}

if ($options & $OPT_LOWER) {
#printf ("lower");
$newfilename = lc ($newfilename);
} elsif ($options & $OPT_UPPER) {
#printf ("upper");
$newfilename = uc ($newfilename);
}

#$oldfilename =~ s/([\.\[\]\(\)\/])/\\$1/g;

printf ("$oldfilename\t-> $newfilename\n");
if (!($options & $OPT_SEMULATE)) {
#printf ("rename ($oldfilename, $newfilename) || die");
if (-e $newfilename) {
printf ("the file rename to '$newfilename' is already exists. overwrite it (Y/N) ");
while (<>) {
if (m/^[Yy]/) {
rename ($oldfilename, $newfilename) || die;
}
}
}
}
}

No comments: