linux下很简单,用iconv或者convmv就行了,windows下就麻烦了,于是手动写了一个。
- use Encode;
-
use Encode::CN;
-
use Encode::Detect::Detector;
-
use Win32::File;
-
-
my @files = <*.lua>;
-
foreach my $file (@files) {
-
my $attr;
-
Win32::File::GetAttributes($file, $attr);
-
my $flag = 1;
-
my $newfile = $file.".tmp";
-
open OLD, $file or die "open $file failed: $!";
-
open NEW, ">$newfile" or die "open $newfile failed: $!";
-
while (my $line = <OLD>) {
-
if(detect($line) eq "UTF-8") {
-
$flag = 0;
-
last;
-
}
-
$line = decode("gb2312", $line);
-
print NEW encode("utf8", $line);
-
}
-
close OLD;
-
close NEW;
-
if($flag == 1) {
-
unlink $file;
-
rename $newfile, $file;
-
Win32::File::SetAttributes($file, $attr)
-
or die "set attribute failed: $!";
-
print "$file change to utf8\n";
-
} else {
-
unlink $newfile;
-
print "$file no change\n";
-
}
-
}
-
-
sleep 1;
阅读(1668) | 评论(0) | 转发(0) |