批量更新xml文件 [英] update xml file with batch

查看:353
本文介绍了批量更新xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我一直在找一个小时没有运气
  2. 我的老板希望将其作为批处理文件

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

I have a xml file that contains the following.

    <?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>

我们想将<smpnumprocs>4</smpnumprocs>(这是使用的处理器数量)设置为2 因此,该行应如下所示:<smpnumprocs>2</smpnumprocs>

We want to set <smpnumprocs>4</smpnumprocs> (which is the number of processors used) to 2 therefore, the line should look like this <smpnumprocs>2</smpnumprocs>

我想出了如何达到我想要的值

I figured out how to get to the value I want with this

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

现在如何更改值?

推荐答案

您可以使用我编写的脚本:

You can use script I wrote:

@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.

Script scans through given xml file and finds line with smpnumprocs in it. If that kind of line is found, it substitutes 4 to 2.

所有行都转储到<xmlFilePathHere>.tmp文件中,该文件将在末尾替换原始文件.

All lines are dumped to <xmlFilePathHere>.tmp file, which replaces original file at the end.

这篇关于批量更新xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆