| #file:batchReplace.pl #author:Bound0
 #created:2007-04-06
 #first published: http://bbs.blueidea.com/viewthread.php?tid=2734388
 
 my $match;
 my $replacement='';
 my $insensitive=0;
 my $global=0;
 my $gi;
 my $go;
 my $Checktype=".htm|.html";
 my $except;
 
 if(open(setfile,"<batchReplace.set"))
 {
 while(<setfile>)
 {
 if(/^\s*-I/){$insensitive=1}
 if(/^\s*-G/){$global=1}
 if(/^\s*-m=(.+)/){$match=$1}
 if(/^\s*-r=(.+)/){$replacement=$1}
 if(/^\s*-e=(.+)/){$except=$1}
 if(/^\s*-i=(.+)/){$gi=$1}
 if(/^\s*-o=(.+)/){$go=$1}
 if(/^\s*-c=(.+)/){$Checktype=$1}
 if(/^\s*Insensitive/){$insensitive=1}
 if(/^\s*Global/){$global=1}
 if(/^\s*Match=(.+)/){$match=$1}
 if(/^\s*Replacement=(.+)/){$replacement=$1}
 if(/^\s*Except=(.+)/){$except=$1}
 if(/^\s*Input=(.+)/){$gi=$1}
 if(/^\s*Output=(.+)/){$go=$1}
 if(/^\s*CheckType=(.+)/){$Checktype=$1}
 }
 }
 
 my $para=' '.join(' ',@ARGV);
 if($para=~/ -I */){$insensitive=1}
 if($para=~/ -G */){$global=1}
 my @ti=split(/ -i */,$para);
 if($ti[1]){($gi)=split(/ -(o|i|c|e|m|r|I|G)/,$ti[1])}
 unless($gi){print "No \"Input path\" parameter!";exit}
 my @to=split(/ -o */,$para);
 if($to[1]){($go)=split(/ -(o|i|c|e|m|r|I|G)/,$to[1])}
 unless($go){print "No \"Output path\" parameter!";exit}
 my @tc=split(/ -c */,$para);
 if($tc[1]){($Checktype)=split(/ -(o|i|c|e|m|r|I|G)/,$tc[1])}
 my @te=split(/ -e */,$para);
 if($te[1]){($except)=split(/ -(o|i|c|e|m|r|I|G)/,$te[1])}
 my @tr=split(/ -r */,$para);
 if($tr[1]){($replacement)=split(/ -(o|i|c|e|m|r|I|G)/,$tr[1])}
 
 unless($match){$match="<iframe[^>]*>[\\s\\S]*?<\\/iframe>";
 $insensitive=1;
 $global=1}
 
 my @tm=split(/ -m */,$para);
 if($tm[1]){($match)=split(/ -(o|i|c|e|m|r|I|G)/,$tm[1])}
 unless($match){print "No \"Match Pattern\" parameter!";exit}
 
 my $checktyp='(';
 $Checktype=~s/\./\\\./g;
 $Checktype=~s/\|/\)\|\(/g;
 $checktyp.=$Checktype.')$';
 
 my $excep;
 if($except){
 $excep=$except;
 $excep=~s/\//\\\//g;
 $excep=~s/\./\\\./g;
 $excep=~s/\|/\\\|/g;
 $excep=~s/\[/\\\[/g;
 $excep=~s/\]/\\\]/g;
 $excep=~s/\(/\\\(/g;
 $excep=~s/\)/\\\)/g;
 $excep=~s/\$/\\\$/g;
 $excep=~s/\?/\\\?/g;
 }
 
 my $replacemen;
 if($replacement){
 $replacemen=$replacement;
 $replacemen=~s/\//\\\//g;
 $replacemen=~s/\./\\\./g;
 $replacemen=~s/\|/\\\|/g;
 $replacemen=~s/\[/\\\[/g;
 $replacemen=~s/\]/\\\]/g;
 $replacemen=~s/\(/\\\(/g;
 $replacemen=~s/\)/\\\)/g;
 $replacemen=~s/\$/\\\$/g;
 $replacemen=~s/\?/\\\?/g;
 }
 
 sub cFile
 {
 my $fi;
 ($fi)=@_;
 if(opendir(DIR, $fi))
 {
 my @dir=readdir(DIR);
 closedir DIR;
 if("\\" eq substr $fi,(length $fi)-1){$fi=substr($fi,0,(length $fi)-1)}
 my @subdirs= grep { /^(?!\.)/ && -d "$fi\\$_" } @dir;
 foreach my $subdir (@subdirs)
 {
 cFile("$fi\\$subdir")
 }
 @files = grep { /$checktyp/i  && -T "$fi\\$_" } @dir;
 foreach my $fil (@files)
 {
 my $bp='';
 $bp=(substr $fi,(length $gi))."\\";
 my $bi="$fi\\$fil";
 my $bo=$go.$bp.$fil;
 remove($bi,$bo)
 }
 }
 }
 unless("\\" eq substr $go,(length $go)-1){$go.="\\"}
 if(-d $gi)
 {
 unless("\\" eq substr $gi,(length $gi)-1){$gi.="\\"}
 cFile($gi);
 }
 else
 {
 my $bu=substr $gi,(rindex $gi,'\\');
 my $bo=$go.$bu;
 remove($gi,$bo)
 }
 
 print "\nProcess Finished!";
 print "\n-i:$gi";
 print "\n-o:$go";
 print "\n-m:$match";
 if($except){print "\n-e:$except"}
 if($replacement){print "\n-r:$replacement"}
 sub remove
 {
 my $bi;
 my $bo;
 ($bi,$bo)=@_;
 
 print "\nprocessing $bi ...\n";
 unless(open(INPUT,"<$bi")){print "\n[Warn] Can not open the file <$bi>: $!";return}
 my @conts = <INPUT>;
 close INPUT;
 my $cont=join '',@conts;
 my $c;
 if($insensitive)
 {
 if($global)
 {
 unless($cont=~s/($match)/${$c=Cexcept($1)}$c/gi){die "$!"}
 }
 else
 {
 unless($cont=~s/($match)/${$c=Cexcept($1)}$c/i){die "$!"}
 }
 }
 else
 {
 if($global)
 {
 unless($cont=~s/($match)/${$c=Cexcept($1)}$c/g){die "$!"}
 }
 else
 {
 unless($cont=~s/($match)/${$c=Cexcept($1)}$c/){die "$!"}
 }
 }
 unless(open(OUT, ">$bo"))
 {
 if($!==2)
 {
 my $dbo=substr $bo,0,(rindex $bo,'\\');
 if(opendir(OUTDIR,$dbo)){closedir OUTDIR;print "\n[Warn] Can not open the output file <$bo>: $!";exit}
 else
 {
 if($!==2)
 {
 unless(pmkpath($dbo)){print "\n[Warn] Can not creat the output directory <$dbo>: $!";exit}
 unless(open(OUT,">>$bo")){print "\n[Warn] Can not open the output file <$bo>: $!";exit}
 }
 else{print "\n[Warn] Can not open the output directory <$dbo>: $!";exit}
 }
 }
 else{print "\n[Warn] Can not open the output file <$bo>: $!";exit}
 }
 
 print OUT "$cont";
 close OUT;
 }
 sub pmkpath
 {
 my @p=split(/\\/,shift);
 my $pa=$p[0];
 my $m=$#p+1;
 my $t;
 for($t=1; -e $pa;$t++){$pa.='\\'.$p[$t]}
 unless(mkdir $pa){return 0}
 for(;$t<$m;$t++)
 {
 $pa.='\\'.$p[$t];
 unless(mkdir $pa){return 0}
 }
 return 1
 }
 sub Cexcept
 {
 unless($except){return $replacemen}
 my $con;
 ($con)=@_;
 if($con=~/$excep/){return $con}else{return $replacemen}
 }
 出处:蓝色理想
责任编辑:moby
 上一页 [Perl]文字/代码批量替换工具 [1] 下一页 测试样例 
	      |