sharpds77 发表于 2015-12-27 08:16:18

Perl查找资源文件

  这几天做一个本地化项目,翻译组要做log纪录很累,以前直听说Perl文本处理的功能很强,一时兴起,就现学现用用perl为他们写了个自动抓取变更纪录的perl程序,发现Perl真的很强大,程序如下:

#!/usr/bin/perl -w
use encoding 'UTF-8', STDIN =>'UTF-8', STDOUT =>'UTF-8';
use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
  $Win32::OLE::Warn = 3;
  #define some parameters
my $LogFilePath="E:/PickUpString/Change Log.xls";
my $excelRowStart=722;
my $excelRowEnd=773;
my $excelCol=2;
my $excelSheet=1;
my $rcFile="legacy";
my $rcPath="F:/RC/NewRC/";
my $rcOldPath="F:/RC/OldRC/";
#end define
  my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
    || Win32::OLE->new('Excel.Application', 'Quit');
   
my $Book = $Excel->Workbooks->Open($LogFilePath);
  my $Sheet = $Book->Worksheets($excelSheet);
  my $stringId="";
  foreach my $row ($excelRowStart..$excelRowEnd)
{
    $stringId=GetStringID($Sheet->Cells($row,$excelCol)->{'Value'},"${rcPath}${rcFile}.rid");
      
    $Sheet->Cells($row,$excelCol+1)->{'Value'}=GetCaption($stringId,
                                                          "${rcOldPath}(SChinese)${rcFile}.rc");
    $Sheet->Cells($row,$excelCol+2)->{'Value'}=GetCaption($stringId,
                                                          "${rcPath}(SChinese)${rcFile}.rc");
    $Sheet->Cells($row,$excelCol+3)->{'Value'}=GetCaption($stringId,
                                                          "${rcPath}${rcFile}.rc");   
}
  $Book->Close;
  sub GetStringID
{
    my ($id,$filePath)=@_;
    my $strValue;
    open(FILE,$filePath) || die "$filePath not exists" ;
      
    while(my $line=<FILE>)
    {
      if($line=~/$id/)
      {
            $line=~/\s.*\s[^\d]/;
            $strValue=$&;
            $strValue=~s/\s//g;
            last;
      }
    }
   
    close(FILE);
  return $strValue;
}
  sub GetCaption
{
    my ($StringID,$filePath)=@_;
    my $strValue;
      
    open(FILE,$filePath) || die "$filePath not exists" ;
  while(my $line=<FILE>)
    {
      if($line=~/$StringID/)
      {
            $line=~/\".*\"/;
            $strValue=$&;
            last;
      }
    }
   
    close(FILE);
   
    return $strValue;
}
  首先从Excel里将ID编号取出来,然后到资源文件中查找对应编号的资源,注此程序只能查找TableString的值。

短短的几十行代码,完成这样的功能使我对脚本语言刮目相看,以后决定好好研究一下脚本语言。
页: [1]
查看完整版本: Perl查找资源文件