本帖最后由 zaqmlp 于 2022-4-14 18:06 编辑
- <# :
- cls&echo off&cd /d "%~dp0"&mode con lines=5000
- set "current=%cd%"
- powershell -NoProfile -ExecutionPolicy bypass "[IO.File]::ReadAllText(\"%~f0\",[Text.Encoding]::GetEncoding('GB2312'))|Invoke-Expression"
- pause
- exit
- #>
- $facelist=@("HarmonyOS Sans SC Bold|12","HarmonyOS Sans SC|15");
-
- $current=get-item -literal $env:current;
- $files=@(dir -literal $current.FullName -recurse|?{($_.Extension -eq '.srt') -and ($_ -is [System.IO.FileInfo])});
-
- $enc=New-Object System.Text.UTF8Encoding $False;
-
- write-host '1、添加';
- write-host '2、清除';
- $choice='';
- while($choice -notmatch '^(1|2)$'){
- $choice=read-host '输入数字序号并回车';
- }
-
- for($i=0;$i -lt $files.length;$i++){
- $text=[IO.File]::ReadAllText($files[$i].FullName, $enc);
- write-host $files[$i].FullName.Substring($current.FullName.length);
- [System.Collections.ArrayList]$s=@();$newtext='';
- $oldtext=[IO.File]::ReadAllText($files[$i].FullName, $enc);
- $arr=$oldtext.trim() -split '[\r\n]{3,}';
- for($j=0;$j -lt $arr.length;$j++){
- $brr=$arr[$j] -split '[\r\n]+';
- for($k=2;$k -le 3;$k++){
- if(-not [string]::IsNullOrEmpty($brr[$k])){
- $m=[regex]::matches($brr[$k], '<font (face|size)=[^>]*?>', 'IgnoreCase');
- if($m.count -ge 1){
- $brr[$k]=$brr[$k] -replace '<font (face|size)=[^>]*?>','';
- foreach($it in $m){
- $brr[$k]=([regex]'(?i)</font>\s*$').replace($brr[$k],'', 1);
- }
- }
- $brr[$k]=$brr[$k] -replace '\{\\fn.+?\\fs\d+?\}',''
- if($choice -eq '1'){
- $crr=$facelist[$k-2].split('|');
- $brr[$k]='{\fn'+$crr[0]+'\fs'+$crr[1]+'}'+$brr[$k];
- }
- }
- }
- $line=$brr -join "`r`n";
- [void]$s.add($line);
- }
- $newtext=$s -join "`r`n`r`n"
- [IO.File]::WriteAllText($files[$i].FullName, $newtext, $enc);
- }
复制代码
|