标题: 【已解决】【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
- <#*,:&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
- #>
- # 替换字符串
- $htstr = @{
- 'persist' = 'const'
- 'persist="true"' = 'const="true"'
- }
- # xml路径
- $xmlPath = ".\xml\*.xml"
- 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 {
- Write-Host $_.FullName
- $encoding = Get-Encoding -Path $_.FullName
- $txt = [System.IO.File]::ReadAllText($_.FullName, $encoding)
- foreach ($key in $htstr.Keys) {
- $txt = $txt.Replace($key, $htstr[$key])
- }
- [System.IO.File]::WriteAllText($_.FullName, $txt, $encoding)
- } finally {
-
- }
- trap {}
- }
- }
复制代码
作者: linfeng_321 时间: 2022-5-9 12:08
回复 2# flashercs
红包转过去了,谢谢大佬!
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |