[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 flashercs 于 2022-7-16 09:20 编辑
  1. <#*,:&cls
  2. @echo off
  3. cd /d "%~dp0"
  4. powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
  5. pause
  6. exit /b
  7. #>
  8. # 删除的注释标签
  9. $arrstr = @(
  10.   '<!--开屏变量播放-->'
  11.   '<!--开屏变量播放2-->'
  12. )
  13. # 文件夹排除
  14. $dirExclude = @(
  15.   "222"
  16. )
  17. # xml路径
  18. $xmlPath = ".\*.xml"
  19. $dirExclude = @($dirExclude | ForEach-Object {
  20.     "*\" + [System.Management.Automation.WildcardPattern]::Escape($_) + "\*"
  21.   })
  22. function Get-Encoding {
  23.   [CmdletBinding(DefaultParameterSetName = "PathSet")]
  24.   param (
  25.     [Parameter(ParameterSetName = "StreamSet", Mandatory = $true)]
  26.     [ValidateNotNullOrEmpty()]
  27.     [System.IO.Stream]$Stream,
  28.     [Parameter(ParameterSetName = "PathSet", Mandatory = $true, Position = 0)]
  29.     [ValidateNotNullOrEmpty()]
  30.     [System.String]$Path,
  31.     [Parameter(Mandatory = $false, Position = 1)]
  32.     [System.UInt32]$ReadCount = 1024
  33.   )
  34.   $utf8BOMThrow = New-Object System.Text.UTF8Encoding -ArgumentList @($true, $true)
  35.   $utf8NoBOMThrow = New-Object System.Text.UTF8Encoding -ArgumentList @($false, $true)
  36.   $utf16LEBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($false, $true, $true)
  37.   $utf16LENoBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($false, $false, $true)
  38.   $utf16BEBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($true, $true, $true)
  39.   $utf16BENoBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($true, $false, $true)
  40.   # type encoding,bool bom,bool throw,Text.Encoding encoding,byte[] preamble,string strPreamble
  41.   $arrUTF8Bom = $utf8BOMThrow.GetPreamble()
  42.   $arrUTF16LEBom = $utf16LEBOMThrow.GetPreamble()
  43.   $arrUTF16BEBom = $utf16BEBOMThrow.GetPreamble()
  44.   
  45.   if ($PSCmdlet.ParameterSetName -eq "PathSet") {
  46.     try {
  47.       $Stream = New-Object System.IO.FileStream -ArgumentList @($Path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read)
  48.     } catch {
  49.       return $null
  50.     }
  51.   }
  52.   $byteBuff = New-Object byte[] -ArgumentList 3
  53.   $readCount = $Stream.Read($byteBuff, 0, 3)
  54.   if ($byteBuff[0] -eq $arrUTF8Bom[0] -and $byteBuff[1] -eq $arrUTF8Bom[1] -and $byteBuff[2] -eq $arrUTF8Bom[2]) {
  55.     # utf8bom
  56.     $return = $utf8BOMThrow
  57.   } elseif ($byteBuff[0] -eq $arrUTF16LEBom[0] -and $byteBuff[1] -eq $arrUTF16LEBom[1]) {
  58.     # utf16lebom
  59.     $return = $utf16LEBOMThrow
  60.   } elseif ($byteBuff[0] -eq $arrUTF16BEBom[0] -and $byteBuff[1] -eq $arrUTF16BEBom[1]) {
  61.     # utf16bebom
  62.     $return = $utf16BEBOMThrow
  63.   } else {
  64.     # nobom
  65.     if ($ReadCount -gt 0) {
  66.       $charBuff = New-Object char[] -ArgumentList $ReadCount
  67.     }
  68.     # utf16-nobom 都被认为是ANSI编码
  69.     foreach ($encoding in @($utf8NoBOMThrow<# , $utf16LENoBOMThrow, $utf16BENoBOMThrow #>)) {
  70.       try {
  71.         $Stream.Position = 0
  72.         $sr = New-Object System.IO.StreamReader -ArgumentList @($Stream, $encoding, $false)
  73.         if ($ReadCount -gt 0) {
  74.           [void]$sr.Read($charBuff, 0, $ReadCount)
  75.         } else {
  76.           [void]$sr.ReadToEnd()
  77.         }
  78.         $return = $encoding
  79.         break
  80.       } catch {
  81.         
  82.       } finally {
  83.         if ($sr) {
  84.           $sr.Dispose()
  85.         }
  86.       }
  87.     }
  88.   }
  89.   if ($PSCmdlet.ParameterSetName -eq "PathSet") {
  90.     $Stream.Dispose()
  91.   }
  92.   if (!$return) {
  93.     $return = [System.Text.Encoding]::Default
  94.   }
  95.   return $return  
  96. }
  97. Get-ChildItem -Path $xmlPath -Filter *.xml -Recurse | ForEach-Object {
  98.   if (-not $_.PSIsContainer) {
  99.     try {
  100.       $flagExclude = $false
  101.       foreach ($itemPattern in $dirExclude ) {
  102.         if ($_.FullName -like $itemPattern) {
  103.           $flagExclude = $true
  104.           break
  105.         }
  106.       }
  107.       if (-not $flagExclude) {
  108.         Write-Host $_.FullName
  109.         $encoding = Get-Encoding -Path $_.FullName
  110.         # $txt = [System.IO.File]::ReadAllText($_.FullName, $encoding)
  111.         $lines = [System.IO.File]::ReadAllLines($_.FullName, $encoding)
  112.         $stack = 0
  113.         [System.IO.File]::WriteAllLines($_.FullName, [string[]]@(for ($i = 0; $i -lt $lines.Count; ++$i) {
  114.               $line = $lines[$i]
  115.               if ($stack -eq 0) {
  116.                 $begin = $false
  117.                 foreach ($key in $arrstr) {
  118.                   if ($line.Contains($key)) {
  119.                     $begin = $true
  120.                     break
  121.                   }
  122.                 }
  123.                 if ($begin) {
  124.                   $stack = 1
  125.                 } else {
  126.                   $line
  127.                 }
  128.               } elseif ($stack -eq 1) {
  129.                 if ($line -match '<!--.*?-->') {
  130.                   $stack = 0
  131.                   $line
  132.                 }
  133.               }
  134.             }), $encoding)
  135.       }
  136.     } finally {
  137.     }
  138.     trap {}
  139.   }
  140. }
复制代码
微信:flashercs
QQ:49908356

TOP

返回列表