Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1145773
  • 博文数量: 341
  • 博客积分: 12744
  • 博客等级: 上将
  • 技术积分: 4040
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-12 09:34
文章分类
文章存档

2014年(1)

2013年(10)

2012年(17)

2011年(63)

2010年(102)

2009年(107)

2008年(41)

分类: WINDOWS

2011-02-18 16:52:17

linux下很简单,用iconv或者convmv就行了,windows下就麻烦了,于是手动写了一个。
  1. use Encode;
  2. use Encode::CN;
  3. use Encode::Detect::Detector;
  4. use Win32::File;

  5. my @files = <*.lua>;
  6. foreach my $file (@files) {
  7.     my $attr;
  8.     Win32::File::GetAttributes($file, $attr);
  9.     my $flag = 1;
  10.     my $newfile = $file.".tmp";
  11.     open OLD, $file or die "open $file failed: $!";
  12.     open NEW, ">$newfile" or die "open $newfile failed: $!";
  13.     while (my $line = <OLD>) {
  14.         if(detect($line) eq "UTF-8") {
  15.             $flag = 0;
  16.             last;
  17.         }
  18.         $line = decode("gb2312", $line);
  19.         print NEW encode("utf8", $line);
  20.     }
  21.     close OLD;
  22.     close NEW;
  23.     if($flag == 1) {
  24.         unlink $file;
  25.         rename $newfile, $file;
  26.         Win32::File::SetAttributes($file, $attr)
  27.             or die "set attribute failed: $!";
  28.         print "$file change to utf8\n";
  29.     } else {
  30.         unlink $newfile;
  31.         print "$file no change\n";
  32.     }
  33. }

  34. sleep 1;

阅读(1580) | 评论(0) | 转发(0) |
0

上一篇:Perl中检测文件编码

下一篇:51cto读书频道

给主人留下些什么吧!~~