bat存为ANSI/GB2312编码- <# :
- cls&echo off&cd /d "%~dp0"
- powershell -NoProfile -ExecutionPolicy bypass "[IO.File]::ReadAllText(\"%~f0\",[Text.Encoding]::GetEncoding('GB2312'))|Invoke-Expression"
- pause
- exit
- #>
- $txtfile=".\词组白名单.txt";
- $newfolder="E:\词组归档";
-
- if(-not (test-path -literal $txtfile)){write-host ('"'+$txtfile+'" 未找到');exit;}
-
- $enc=[Text.Encoding]::GetEncoding('GB2312');
- $dic=New-Object 'System.Collections.Generic.Dictionary[string, Object]';
- $text=[IO.File]::ReadAllLines($txtfile, $enc);
- for($i=0;$i -lt $text.length;$i++){
- $key=$text[$i].trimend().ToLower().split('=');
- if(-not $dic.ContainsKey($key[0])){
- [System.Collections.ArrayList]$brr=@();
- $dic.add($key[0], $brr);
- }
- for($j=0;$j -lt $key.length;$j++){
- [void]$dic[$key[0]].add($key[$j]);
- }
- }
-
- $folders=@(dir -literal "."|?{$_ -is [System.IO.DirectoryInfo]});
- for($i=0;$i -lt $folders.length;$i++){
- write-host $folders[$i].FullName;
- $files=@(dir -literal $folders[$i].FullName|?{('.srt' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
- for($j=0;$j -lt $files.length;$j++){
- write-host $files[$j].Name;
- $oldtext=[IO.File]::ReadAllText($files[$j].FullName, $enc);
- $crr=$oldtext.trim() -split '[\r\n]+';
- if($crr.length -ge 3){
- $m1=[regex]::match($crr[2], '(?i)<font( [^>]+?)?>');
- if($m1.Success){
- $crr[2]=($crr[2] -replace ' {2,}',' ') -replace '<[^>]+?>','';
- }
-
- foreach($k in $dic.Keys){
- for($p=0;$p -lt $dic[$k].Count;$p++){
- $findword=$dic[$k][$p].ToString() -replace '\bbe\b','(am|is|are|were)';
- $findword=$findword -replace "\b(sb\.|sth\.|one's)(?=\s|$)",'\S+';
- $findword=$findword -replace '\bdoing\b','\S+ing';
- $reg='\b'+$findword+'(?=[^a-zA-Z]|$)';
- $m2=[regex]::match($crr[2], $reg);
- if($m2.Success){
- $drr=$crr.Clone();
- $newword='[b]<font color="#ff0000">'+$m2.groups[0].value+'</font>[\b]';
- $drr[2]=$crr[2].replace($m2.groups[0].value, $newword);
- if($m1.Success){
- $drr[2]=$m1.groups[0].value+$crr[2].replace($m2.groups[0].value, $newword)+'</font>';
- }
- $newpath=$newfolder.trimend('\')+'\'+$k;
- if(-not (test-path -literal $newpath)){[void][IO.Directory]::CreateDirectory($newpath);}
- $newbasename="[{0}][{1}][{2}]" -f $k,$folders[$i].Name,(get-date).ToString('yyyy-MM-dd HH点mm分ss秒fff毫秒');
- $newsrt=$newpath+'\'+$newbasename+$files[$j].Extension;
- $newtext=($drr -join "`r`n")+"`r`n`r`n"+'2'+"`r`n"+$crr[1]+"`r`n"+'{\an9}[b]<font color="#cccccc">'+$folders[$i].Name+'</font>[\b]';
- [IO.File]::WriteAllText($newsrt, $newtext, $enc);
- $newmp4=$newpath+'\'+$newbasename+'.mp4';
- $oldmp4=$folders[$i].FullName+'\'+$files[$j].BaseName+'..mp4';
- if(test-path -liter $oldmp4){
- copy-item -literal $oldmp4 $newmp4 -force;
- }else{
- $oldmp4=$folders[$i].FullName+'\'+$files[$j].BaseName+'.mp4';
- if(test-path -liter $oldmp4){
- copy-item -literal $oldmp4 $newmp4 -force;
- }
- }
- }
- }
- }
- }
- }
- }
复制代码
|