问题描述
- 我一直在找一个小时没有运气
- 我的老板希望将其作为批处理文件
我有一个包含以下内容的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 2therefore, 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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!