把 绝对路径 作为脚本参数传递给 PowerShell 脚本
get-ChildItem(dir)、get-Content(gc)、set-Content(sc) 这些 cmdlet 用参数 -LiteralPath 代替 -Path
当路径或文件名出现"["字符时可以避免报错。- <# :
- @echo off
- PowerShell -c ". ([ScriptBlock]::Create((gc -Literal '%~f0') -join \"`r`n\")) '%~dp0'"
- pause & exit/b
- #>
-
- param([string]$path);
- $reg = '^(\d\d:\d\d:\d\d,\d\d\d)\s*-->\s*(\d\d:\d\d:\d\d,\d\d\d)$';
-
- forEach( $file In (dir -Literal $path -Filter *.srt) ){
- $arr = gc -Literal $file.FullName -Enc UTF8 -ReadCount 0;
- $count = $arr.Count;
- for( $i = 0; $i -lt $count; $i++ ){
- if( $arr[$i] -match $reg ){
- $T1 = [DateTime]$matches[1].Replace(',', '.');
- $T2 = [DateTime]$matches[2].Replace(',', '.');
- if( ($T2 - $T1).TotalSeconds -gt 5 ){
- $arr[$i] = $matches[1] + ' --> ' + $T1.AddSeconds(5).ToString('HH:mm:ss,fff');
- }
- }
- }
- sc -Literal $file.FullName -Enc UTF8 -Value $arr;
- }
复制代码
|