找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 13262|回复: 4

请求删除此帖,废弃无用了请求删除此帖,废弃无用了

  [复制链接]
发表于 2022-3-18 22:27:07 | 显示全部楼层 |阅读模式
本帖最后由 cl2039996 于 2022-5-8 16:29 编辑

请求删除此帖,废弃无用了请求删除此帖,废弃无用了请求删除此帖,废弃无用了请求删除此帖,废弃无用了
发表于 2022-3-19 10:06:44 | 显示全部楼层
bat存为ANSI/GB2312编码
  1. <# :
  2. cls&echo off&cd /d "%~dp0"&mode con lines=5000
  3. set "current=%cd%"
  4. powershell -NoProfile -ExecutionPolicy bypass "[IO.File]::ReadAllText("%~f0",[Text.Encoding]::GetEncoding('GB2312'))|Invoke-Expression"
  5. pause
  6. exit
  7. #>
  8. $current=get-item -literal $env:current;
  9. $files=@(dir -literal $current.FullName -recurse|?{($_.Extension -eq '.srt') -and ($_ -is [System.IO.FileInfo])});

  10. $enc=New-Object System.Text.UTF8Encoding $False;
  11. [System.Collections.ArrayList]$filelist=@();
  12. for($i=0;$i -lt $files.length;$i++){
  13.     $text=[IO.File]::ReadAllText($files[$i].FullName, $enc);
  14.     $m=[regex]::match($text, '(?i)\{\\pos\(\d+ ?, ?\d+\)\}');
  15.     if($m.Success){
  16.         [void]$filelist.add($files[$i]);
  17.         write-host ($m.groups[0].value+'  '+$files[$i].FullName.Substring($current.FullName.length));
  18.     }
  19. }

  20. if($filelist.Count -ge 1){
  21.     write-host '1、修改位置标签';
  22.     write-host '2、清除位置标签';
  23.    
  24.     $choice1='';
  25.     while($choice1 -notmatch '^(1|2)$'){
  26.         $choice1=read-host '输入数字序号并回车';
  27.     }
  28.    
  29.     $pos='';
  30.     if($choice1 -eq '1'){
  31.         $x_value=read-host '输入x坐标并回车';
  32.         $y_value=read-host '输入y坐标并回车';
  33.         if(($x_value -notmatch '^\d+$') -or ($y_value -notmatch '^\d+$')){
  34.             write-host '输入有误';exit;
  35.         }
  36.         $pos='{\pos('+$x_value+','+$y_value+')}';
  37.     }
  38.     clear-host;
  39.     for($i=0;$i -lt $filelist.Count;$i++){
  40.         write-host $filelist[$i].FullName.Substring($current.FullName.length);
  41.         $text=[IO.File]::ReadAllText($filelist[$i].FullName, $enc);
  42.         $text=$text -replace '\{\\pos\(\d+ ?, ?\d+\)\}', $pos;
  43.         [IO.File]::WriteAllText($filelist[$i].FullName, $text, $enc);
  44.     }
  45. }else{
  46.     $pos='';
  47.     $x_value=read-host '输入x坐标并回车';
  48.     $y_value=read-host '输入y坐标并回车';
  49.     if(($x_value -notmatch '^\d+$') -or ($y_value -notmatch '^\d+$')){
  50.         write-host '输入有误';exit;
  51.     }
  52.     $pos='{\pos('+$x_value+','+$y_value+')}';
  53.    
  54.     for($i=0;$i -lt $files.length;$i++){
  55.         write-host $files[$i].FullName.Substring($current.FullName.length);
  56.         [System.Collections.ArrayList]$newtext=@();
  57.         $oldtext=[IO.File]::ReadAllText($files[$i].FullName, $enc);
  58.         $arr=$oldtext.trim() -split '[\r\n]{3,}';
  59.         for($j=0;$j -lt $arr.length;$j++){
  60.             $brr=$arr[$j] -split '[\r\n]+';
  61.             if(-not [string]::IsNullOrEmpty($brr[2])){
  62.                 if(($brr[2] -notmatch '\{\\an\d+\}')){
  63.                     $brr[2]=$pos+$brr[2];
  64.                 }
  65.             }
  66.             $line=$brr -join "`r`n";
  67.             [void]$newtext.add($line);
  68.         }
  69.         [IO.File]::WriteAllText($files[$i].FullName, $($newtext -join "`r`n`r`n"), $enc);
  70.     }
  71. }
复制代码
 楼主| 发表于 2022-3-19 16:50:32 | 显示全部楼层
本帖最后由 cl2039996 于 2022-3-19 16:53 编辑

回复 2# zaqmlp

您好,经测试,各项功能符合预期,非常感激!可是,有一个棘手的问题是:

执行bat之后,pos位置标签完美添加上了,可是意外发现,所有srt中的中文字幕全部变成乱码,如下面红色所示:
1
00:00:00,000 --> 00:00:03,360
{\pos(300,400)}<font color="#00ff00">and then, <b><font color="#ff0000">all of a sudden</font><\b>, out of nowhere, they just...</font>
<font color="#00ff00">&#571;&#65533;&#65533;&#891;&#571;&#1454;&#65533;&#18668;</font> <font color="#00ff00">&#65533;&#65533;&#1450;&#65533;&#1274;ζ&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#510;&#865;&#65533;&#65533;&#65533;</font>

2
00:00:00,000 --> 00:00:03,360
{\an9}<b><font color="#cccccc">Biff and Chip - Series 1- 5. A Sticky Situation_batch</font><\b>

请求大佬给修一下,您之前写的各种bat没有出现这种现象。修复bug后红包立刻转给您
发表于 2022-3-19 16:55:36 | 显示全部楼层
本帖最后由 zaqmlp 于 2022-3-19 17:03 编辑

回复 3# cl2039996
以后提需求时打包测试文件用网盘分享出来

$enc=New-Object System.Text.UTF8Encoding $False;
改为
$enc=[Text.Encoding]::GetEncoding('GB2312');
 楼主| 发表于 2022-3-19 17:27:37 | 显示全部楼层
回复 4# zaqmlp


    遵命大佬,是我着急了,以后我上传供测试的文件。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 04:07 , Processed in 0.016534 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表