【问题标题】:update xml file with batch批量更新 xml 文件
【发布时间】:2012-06-20 20:47:27
【问题描述】:
  1. 我一直在寻找一个小时没有运气
  2. 我的老板希望它是一个批处理文件

我有一个包含以下内容的 xml 文件。

    <?xml version="1.0"?>
    <profiledoc default="*** Last Run ***">
    <profile name="*** Last Run ***" >
    <workingdir>c:\proj</workingdir>
    <somestuff>none</somestuff>
    <smpnumprocs>4</smpnumprocs>
    <otherstuff></otherstuff>
    <llama>FLUFFY</llama>
    <language>en-us</language>
    <customexe></customexe>
    <addlparams></addlparams>
    <graphicsdevice>win32</graphicsdevice>
    </profile>
    </profiledoc>

我们想将&lt;smpnumprocs&gt;4&lt;/smpnumprocs&gt;(即使用的处理器数量)设置为 2 因此,该行应如下所示&lt;smpnumprocs&gt;2&lt;/smpnumprocs&gt;

我想出了如何用这个来达到我想要的价值

FOR /f "tokens=3 delims=><  " %%a IN ('TYPE %LOCAL_FILE% ^| FIND "<smpnumprocs>"') DO SET NUM_PROCS=%%a

现在如何更改值?

【问题讨论】:

标签: xml windows file batch-file insert-update


【解决方案1】:

你可以使用我写的脚本:

@echo OFF
@setlocal ENABLEDELAYEDEXPANSION

if "%~1" == "" (
    echo Please provide xml file path as a first parameter.
    exit /B 1
)

if not exist "%~1" (
    echo Xml file with given path does not exist.
    exit /B 2
)

if exist "%~1.tmp" del /F /Q "%~1.tmp"

for /F "delims=" %%G in (%~1) do (
    set LINE=%%G
    if not "!LINE!" == "!LINE:smpnumprocs=!" (
        set LINE=!LINE:4=2!
    )
    >> "%~1.tmp" echo !LINE!
)

del /F /Q "%~1"
ren "%~1.tmp" "%~1"

@endlocal

脚本扫描给定的 xml 文件并在其中找到带有 smpnumprocs 的行。如果找到了那种线,它会将 4 替换为 2。

所有行都转储到&lt;xmlFilePathHere&gt;.tmp 文件,该文件最后替换原始文件。

【讨论】:

  • 这将产生空行、带有文本ONOFF/? 的行的问题,并从Hello ! A caret^ 等行中删除字符
  • 这是为给定的 xml 文件设计的解决方案。里面没有这样的字符。
猜你喜欢
  • 2018-02-05
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-04
  • 1970-01-01
  • 2017-08-13
相关资源
最近更新 更多