Board logo

标题: 【已解决】【50元红包】指定路径所有子文件夹里xml文件修改参数替换 [打印本页]

作者: linfeng_321    时间: 2022-5-9 11:09     标题: 【已解决】【50元红包】指定路径所有子文件夹里xml文件修改参数替换

本帖最后由 linfeng_321 于 2022-5-9 12:09 编辑

需求:指定路径所有子文件夹里xml文件修改参数替换

1、persist  改成 const
2、persist="true"  改成 const="true"
3、我可以设置更多参数,进行xml文件里所有对应参数替换
作者: flashercs    时间: 2022-5-9 11:53

  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. $htstr = @{
  10.   'persist'        = 'const'
  11.   'persist="true"' = 'const="true"'
  12. }
  13. # xml路径
  14. $xmlPath = ".\xml\*.xml"
  15. function Get-Encoding {
  16.   [CmdletBinding(DefaultParameterSetName = "PathSet")]
  17.   param (
  18.     [Parameter(ParameterSetName = "StreamSet", Mandatory = $true)]
  19.     [ValidateNotNullOrEmpty()]
  20.     [System.IO.Stream]$Stream,
  21.     [Parameter(ParameterSetName = "PathSet", Mandatory = $true, Position = 0)]
  22.     [ValidateNotNullOrEmpty()]
  23.     [System.String]$Path,
  24.     [Parameter(Mandatory = $false, Position = 1)]
  25.     [System.UInt32]$ReadCount = 1024
  26.   )
  27.   $utf8BOMThrow = New-Object System.Text.UTF8Encoding -ArgumentList @($true, $true)
  28.   $utf8NoBOMThrow = New-Object System.Text.UTF8Encoding -ArgumentList @($false, $true)
  29.   $utf16LEBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($false, $true, $true)
  30.   $utf16LENoBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($false, $false, $true)
  31.   $utf16BEBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($true, $true, $true)
  32.   $utf16BENoBOMThrow = New-Object System.Text.UnicodeEncoding -ArgumentList @($true, $false, $true)
  33.   # type encoding,bool bom,bool throw,Text.Encoding encoding,byte[] preamble,string strPreamble
  34.   $arrUTF8Bom = $utf8BOMThrow.GetPreamble()
  35.   $arrUTF16LEBom = $utf16LEBOMThrow.GetPreamble()
  36.   $arrUTF16BEBom = $utf16BEBOMThrow.GetPreamble()
  37.   
  38.   if ($PSCmdlet.ParameterSetName -eq "PathSet") {
  39.     try {
  40.       $Stream = New-Object System.IO.FileStream -ArgumentList @($Path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read)
  41.     } catch {
  42.       return $null
  43.     }
  44.   }
  45.   $byteBuff = New-Object byte[] -ArgumentList 3
  46.   $readCount = $Stream.Read($byteBuff, 0, 3)
  47.   if ($byteBuff[0] -eq $arrUTF8Bom[0] -and $byteBuff[1] -eq $arrUTF8Bom[1] -and $byteBuff[2] -eq $arrUTF8Bom[2]) {
  48.     # utf8bom
  49.     $return = $utf8BOMThrow
  50.   } elseif ($byteBuff[0] -eq $arrUTF16LEBom[0] -and $byteBuff[1] -eq $arrUTF16LEBom[1]) {
  51.     # utf16lebom
  52.     $return = $utf16LEBOMThrow
  53.   } elseif ($byteBuff[0] -eq $arrUTF16BEBom[0] -and $byteBuff[1] -eq $arrUTF16BEBom[1]) {
  54.     # utf16bebom
  55.     $return = $utf16BEBOMThrow
  56.   } else {
  57.     # nobom
  58.     if ($ReadCount -gt 0) {
  59.       $charBuff = New-Object char[] -ArgumentList $ReadCount
  60.     }
  61.     # utf16-nobom 都被认为是ANSI编码
  62.     foreach ($encoding in @($utf8NoBOMThrow<# , $utf16LENoBOMThrow, $utf16BENoBOMThrow #>)) {
  63.       try {
  64.         $Stream.Position = 0
  65.         $sr = New-Object System.IO.StreamReader -ArgumentList @($Stream, $encoding, $false)
  66.         if ($ReadCount -gt 0) {
  67.           [void]$sr.Read($charBuff, 0, $ReadCount)
  68.         } else {
  69.           [void]$sr.ReadToEnd()
  70.         }
  71.         $return = $encoding
  72.         break
  73.       } catch {
  74.         
  75.       } finally {
  76.         if ($sr) {
  77.           $sr.Dispose()
  78.         }
  79.       }
  80.     }
  81.   }
  82.   if ($PSCmdlet.ParameterSetName -eq "PathSet") {
  83.     $Stream.Dispose()
  84.   }
  85.   if (!$return) {
  86.     $return = [System.Text.Encoding]::Default
  87.   }
  88.   return $return  
  89. }
  90. Get-ChildItem -Path $xmlPath -Filter *.xml -Recurse | ForEach-Object {
  91.   if (-not $_.PSIsContainer) {
  92.     try {
  93.       Write-Host $_.FullName
  94.       $encoding = Get-Encoding -Path $_.FullName
  95.       $txt = [System.IO.File]::ReadAllText($_.FullName, $encoding)
  96.       foreach ($key in $htstr.Keys) {
  97.         $txt = $txt.Replace($key, $htstr[$key])
  98.       }
  99.       [System.IO.File]::WriteAllText($_.FullName, $txt, $encoding)
  100.     } finally {
  101.     }
  102.     trap {}
  103.   }
  104. }
复制代码

作者: linfeng_321    时间: 2022-5-9 12:08

回复 2# flashercs

红包转过去了,谢谢大佬!




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2