Here's a very simple Perl program on UNIX that does the trick. It's not at all well written so it's just for your reference. It will not work unless you make necessary modification (following the CHANGEME keyword.) Run it in every directory with the problem, especially the pages, and attic directories. Also, remember to backup everything before doing so.
#!/usr/local/bin/perl
# CHANGEME
$SEPCHAR="_";
@files=glob("*");
foreach $file (@files){
# decode the file name to original text.
$name=urldecode($file);
$newname=convertname($name);
if($name ne $newname){
handlechange($name, $newname);
}
}
########################
sub urldecode {
my $theURL = $_[0];
$theURL =~ tr/+/ /;
$theURL =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
$theURL =~ s/<!??.|\n)*??//g;
return $theURL;
}
########################
sub urlencode {
my $theURL = $_[0];
$theURL =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
return $theURL;
}
########################
sub convertname{
my $name=$_[0];
# CHANGEME
# Make sure the following covers all your cases of separate characters.
$name=~s/?/$SEPCHAR/g;
$name=~s/?/$SEPCHAR/g;
$name=~s/?/$SEPCHAR/g;
$name=~s/?/$SEPCHAR/g;
$name=~s/?/$SEPCHAR/g;
$name=~s/?/$SEPCHAR/g;
$name=~s/?/$SEPCHAR/g;
return $name;
}
########################
sub handlechange{
my $name=$_[0];
my $newname=$_[1];
my $displayname=$name;
my $displaynewname=$newname;
my $namefile;
my $newnamefile;
# CHANGEME
# display in big5 encoding
$displayname=`echo "$name" |iconv -f utf-8 -t big5`;
chomp($displayname);
# $displayname=$name; // use this one if your display encoding is same as file content.
$displaynewname=`echo "$newname" |iconv -f utf-8 -t big5`;
chomp($displaynewname);
# $displaynewname=$newname; // use this one if your display encoding is same as file content.
$name=~m/(^.*?)\.(.*$)/;
$namefile=urlencode($1).".".$2;
$newname=~m/(^.*?)\.(.*$)/;
$newnamefile=urlencode($1).".".$2;
print "Rename $displayname to $displaynewname? (y/n) y is default.\n";
$input=<STDIN>;
chomp($input);
if($input ne 'n' && $input ne 'N'){
if(-e $newnamefile){
print "!! $displaynewname already exists! !!\n";
#unlink($newnamefile);
}else{
if(rename($namefile,$newnamefile)){
print "Done.\n";
}else{
print "!! Error renaming file. !!\n";
}
}
}
}
########################