本帖最后由 flashercs 于 2022-7-16 09:20 编辑
- <#*,:&cls
- @echo off
- cd /d "%~dp0"
- powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
- pause
- exit /b
- #>
- # 删除的注释标签
- $arrstr = @(
- '<!--开屏变量播放-->'
- '<!--开屏变量播放2-->'
- )
- # 文件夹排除
- $dirExclude = @(
- "222"
- )
- # xml路径
- $xmlPath = ".\*.xml"
- $dirExclude = @($dirExclude | ForEach-Object {
- "*\" + [System.Management.Automation.WildcardPattern]::Escape($_) + "\*"
- })
- function Get-Encoding {
- [CmdletBinding(DefaultParameterSetName = "PathSet")]
- param (
- [Parameter(ParameterSetName = "StreamSet", Mandatory = $true)]
- [ValidateNotNullOrEmpty()]
- [System.IO.Stream]$Stream,
- [Parameter(ParameterSetName = "PathSet", Mandatory = $true, Position = 0)]
- [ValidateNotNullOrEmpty()]
- [System.String]$Path,
- [Parameter(Mandatory = $false, Position = 1)]
- [System.UInt32]$ReadCount = 1024
- )
- $utf8BOMThrow = New-Object System.Text.UTF8Encoding -ArgumentList @($true, $true)
- $utf8NoBOMThrow = New-Object System.Text.UTF8Encoding -ArgumentList @($false, $true)
- $utf16LEBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($false, $true, $true)
- $utf16LENoBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($false, $false, $true)
- $utf16BEBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($true, $true, $true)
- $utf16BENoBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($true, $false, $true)
- # type encoding,bool bom,bool throw,Text.Encoding encoding,byte[] preamble,string strPreamble
- $arrUTF8Bom = $utf8BOMThrow.GetPreamble()
- $arrUTF16LEBom = $utf16LEBOMThrow.GetPreamble()
- $arrUTF16BEBom = $utf16BEBOMThrow.GetPreamble()
-
- if ($PSCmdlet.ParameterSetName -eq "PathSet") {
- try {
- $Stream = New-Object System.IO.FileStream -ArgumentList @($Path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read)
- } catch {
- return $null
- }
- }
- $byteBuff = New-Object byte[] -ArgumentList 3
- $readCount = $Stream.Read($byteBuff, 0, 3)
- if ($byteBuff[0] -eq $arrUTF8Bom[0] -and $byteBuff[1] -eq $arrUTF8Bom[1] -and $byteBuff[2] -eq $arrUTF8Bom[2]) {
- # utf8bom
- $return = $utf8BOMThrow
- } elseif ($byteBuff[0] -eq $arrUTF16LEBom[0] -and $byteBuff[1] -eq $arrUTF16LEBom[1]) {
- # utf16lebom
- $return = $utf16LEBOMThrow
- } elseif ($byteBuff[0] -eq $arrUTF16BEBom[0] -and $byteBuff[1] -eq $arrUTF16BEBom[1]) {
- # utf16bebom
- $return = $utf16BEBOMThrow
- } else {
- # nobom
- if ($ReadCount -gt 0) {
- $charBuff = New-Object char[] -ArgumentList $ReadCount
- }
- # utf16-nobom 都被认为是ANSI编码
- foreach ($encoding in @($utf8NoBOMThrow<# , $utf16LENoBOMThrow, $utf16BENoBOMThrow #>)) {
- try {
- $Stream.Position = 0
- $sr = New-Object System.IO.StreamReader -ArgumentList @($Stream, $encoding, $false)
- if ($ReadCount -gt 0) {
- [void]$sr.Read($charBuff, 0, $ReadCount)
- } else {
- [void]$sr.ReadToEnd()
- }
- $return = $encoding
- break
- } catch {
-
- } finally {
- if ($sr) {
- $sr.Dispose()
- }
- }
- }
- }
- if ($PSCmdlet.ParameterSetName -eq "PathSet") {
- $Stream.Dispose()
- }
- if (!$return) {
- $return = [System.Text.Encoding]::Default
- }
- return $return
- }
-
- Get-ChildItem -Path $xmlPath -Filter *.xml -Recurse | ForEach-Object {
- if (-not $_.PSIsContainer) {
- try {
- $flagExclude = $false
- foreach ($itemPattern in $dirExclude ) {
- if ($_.FullName -like $itemPattern) {
- $flagExclude = $true
- break
- }
- }
- if (-not $flagExclude) {
- Write-Host $_.FullName
- $encoding = Get-Encoding -Path $_.FullName
- # $txt = [System.IO.File]::ReadAllText($_.FullName, $encoding)
- $lines = [System.IO.File]::ReadAllLines($_.FullName, $encoding)
- $stack = 0
- [System.IO.File]::WriteAllLines($_.FullName, [string[]]@(for ($i = 0; $i -lt $lines.Count; ++$i) {
- $line = $lines[$i]
- if ($stack -eq 0) {
- $begin = $false
- foreach ($key in $arrstr) {
- if ($line.Contains($key)) {
- $begin = $true
- break
- }
- }
- if ($begin) {
- $stack = 1
- } else {
- $line
- }
- } elseif ($stack -eq 1) {
- if ($line -match '<!--.*?-->') {
- $stack = 0
- $line
- }
- }
- }), $encoding)
- }
- } finally {
-
- }
- trap {}
- }
- }
复制代码
|