[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 flashercs 于 2020-2-18 20:24 编辑

editxml.bat
只要替换内容即可,不需替换的有什么用?
  1. <#*,:&cls
  2. @echo off
  3. pushd "%~dp0"
  4. powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
  5. popd
  6. pause
  7. exit /b
  8. #>
  9. # modify xml attributes
  10. # 替换内容,格式必须遵守下面样本,不要前加 Note
  11. $hashAttr = @{
  12.   'Bar="9" Pos="12"'        = 'Bar="9" Pos="18"'
  13.   'Bar="9" Pos="40"'        = 'Bar="9" Pos="60"'
  14.   'EndBar="10" EndPos="16"' = 'EndBar="10" EndPos="56"'
  15. }
  16. $xmlfile = "4.xml"
  17. $xmlfilenew = "4_new.xml"
  18. $hashValue = @{ }
  19. $re = [regex]'(\w+)="([^"]*)"'
  20. $xmldoc = New-Object -TypeName System.Xml.XmlDocument
  21. try {
  22.   $xmldoc.Load($xmlfile)
  23.   foreach ($strKey in $hashAttr.Keys) {
  24.     $strXPath = $strKey -replace '\w+=', '@$&' -replace ' @', ' and @'
  25.     $hashValue.Clear()
  26.     foreach ($reMatch in $re.Matches($hashAttr.Item($strKey))) {
  27.       $hashValue.Add($reMatch.Groups[1].Value, $reMatch.Groups[2].Value)
  28.     }
  29.     foreach ($node in $xmldoc.DocumentElement.SelectNodes(".//Note[$strXPath]")) {
  30.       foreach ($dicEntry in $hashValue.GetEnumerator()) {
  31.         $node.SetAttribute($dicEntry.Key, $dicEntry.Value)
  32.       }
  33.     }
  34.   }
  35.   $xmldoc.Save($xmlfilenew)
  36. } catch {
  37.   $_ | Out-String | Write-Host -ForegroundColor Red
  38. }
复制代码
1

评分人数

    • aducyl: 很给力,缺点是产生了多次的循环替换技术 + 1
微信:flashercs
QQ:49908356

TOP

回复 3# aducyl
  1. <#*,:&cls
  2. @echo off
  3. pushd "%~dp0"
  4. powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
  5. popd
  6. pause
  7. exit /b
  8. #>
  9. # modify xml attributes
  10. # 替换内容,格式必须遵守下面样本,不要前加 Note
  11. $hashAttr = @{
  12.   'Bar="5" Pos="0"'        = 'Bar="5" Pos="0"'
  13.   'Bar="5" Pos="2"'        = 'Bar="5" Pos="2"'
  14.   'Bar="5" Pos="4"'        = 'Bar="5" Pos="6"'
  15.   'Bar="5" Pos="6"'        = 'Bar="5" Pos="8"'
  16.   'Bar="5" Pos="8"'        = 'Bar="5" Pos="12"'
  17.   'Bar="5" Pos="10"'       = 'Bar="5" Pos="14"'
  18.   'Bar="5" Pos="12"'       = 'Bar="5" Pos="18"'
  19.   'Bar="5" Pos="14"'       = 'Bar="5" Pos="20"'
  20.   'Bar="5" Pos="16"'       = 'Bar="5" Pos="24"'
  21.   'Bar="5" Pos="18"'       = 'Bar="5" Pos="26"'
  22.   'Bar="5" Pos="20"'       = 'Bar="5" Pos="30"'
  23.   'Bar="5" Pos="22"'       = 'Bar="5" Pos="32"'
  24.   'Bar="5" Pos="24"'       = 'Bar="5" Pos="36"'
  25.   'Bar="5" Pos="26"'       = 'Bar="5" Pos="38"'
  26.   'Bar="5" Pos="28"'       = 'Bar="5" Pos="42"'
  27.   'Bar="5" Pos="30"'       = 'Bar="5" Pos="44"'
  28.   'Bar="5" Pos="32"'       = 'Bar="5" Pos="48"'
  29.   'Bar="5" Pos="34"'       = 'Bar="5" Pos="50"'
  30.   'Bar="5" Pos="36"'       = 'Bar="5" Pos="54"'
  31.   'Bar="5" Pos="38"'       = 'Bar="5" Pos="56"'
  32.   'Bar="5" Pos="40"'       = 'Bar="5" Pos="60"'
  33.   'Bar="5" Pos="42"'       = 'Bar="5" Pos="62"'
  34.   'Bar="5" Pos="44"'       = 'Bar="6" Pos="2"'
  35.   'Bar="5" Pos="46"'       = 'Bar="6" Pos="4"'
  36.   'Bar="5" Pos="48"'       = 'Bar="6" Pos="8"'
  37.   'Bar="5" Pos="50"'       = 'Bar="6" Pos="10"'
  38.   'Bar="5" Pos="52"'       = 'Bar="6" Pos="14"'
  39.   'Bar="5" Pos="54"'       = 'Bar="6" Pos="16"'
  40.   'Bar="5" Pos="56"'       = 'Bar="6" Pos="20"'
  41.   'Bar="5" Pos="58"'       = 'Bar="6" Pos="22"'
  42.   'Bar="5" Pos="60"'       = 'Bar="6" Pos="26"'
  43.   'Bar="5" Pos="62"'       = 'Bar="6" Pos="28"'
  44.   'Bar="6" Pos="0"'        = 'Bar="6" Pos="32"'
  45.   'EndBar="5" EndPos="0"'  = 'EndBar="5" EndPos="0"'
  46.   'EndBar="5" EndPos="2"'  = 'EndBar="5" EndPos="2"'
  47.   'EndBar="5" EndPos="4"'  = 'EndBar="5" EndPos="6"'
  48.   'EndBar="5" EndPos="6"'  = 'EndBar="5" EndPos="8"'
  49.   'EndBar="5" EndPos="8"'  = 'EndBar="5" EndPos="12"'
  50.   'EndBar="5" EndPos="10"' = 'EndBar="5" EndPos="14"'
  51.   'EndBar="5" EndPos="12"' = 'EndBar="5" EndPos="18"'
  52.   'EndBar="5" EndPos="14"' = 'EndBar="5" EndPos="20"'
  53.   
  54. }
  55. $xmlfile = "1.xml"
  56. $xmlfilenew = "1_new.xml"
  57. $hashValue = @{ }
  58. $hashModified = @{ }
  59. $re = [regex]'(\w+)="([^"]*)"'
  60. $xmldoc = New-Object -TypeName System.Xml.XmlDocument
  61. try {
  62.   $xmldoc.Load($xmlfile)
  63.   foreach ($strKey in $hashAttr.Keys) {
  64.     $strXPath = $strKey -replace '\w+=', '@$&' -replace ' @', ' and @'
  65.     $strValue = $hashAttr.Item($strKey)
  66.     $hashValue.Clear()
  67.     foreach ($reMatch in $re.Matches($strValue)) {
  68.       $hashValue.Add($reMatch.Groups[1].Value, $reMatch.Groups[2].Value)
  69.     }
  70.     if (!$hashModified.ContainsKey($strValue)) {
  71.       [void]$hashModified.Add($strValue, (New-Object System.Collections.ArrayList))
  72.     }
  73.     if (!$hashModified.ContainsKey($strKey)) {
  74.       [void]$hashModified.Add($strKey, (New-Object System.Collections.ArrayList))
  75.     }
  76.     $alDst = $hashModified.Item($strValue)
  77.     $alSrc = $hashModified.Item($strKey)
  78.     foreach ($node in $xmldoc.DocumentElement.SelectNodes(".//Note[$strXPath]")) {
  79.       if (!$alSrc.Contains($node)) {
  80.         foreach ($dicEntry in $hashValue.GetEnumerator()) {
  81.           $node.SetAttribute($dicEntry.Key, $dicEntry.Value)
  82.         }
  83.         [void]$alDst.Add($node)
  84.       }
  85.     }
  86.   }
  87.   $xmldoc.Save($xmlfilenew)
  88. } catch {
  89.   $_ | Out-String | Write-Host -ForegroundColor Red
  90. }
复制代码
1

评分人数

    • aducyl: 感谢给帖子标题标注[已解决]字样技术 + 1
微信:flashercs
QQ:49908356

TOP

返回列表