批量替换并将1添加到xml文件中特定文本之间的数字 [英] Batch Replace and add 1 to a number between specific text in an xml file
问题描述
好吧,我正在尝试写一个批处理来为用户抢"一个序列号.序列号来自另一个系统,我们必须定期手动输入序列号.我希望用户运行批处理文件,该文件将加1并替换旧数字,然后回显您的序列号为##",其中##是新数字.我很确定我可以计算出Echo部分并写入临时文件并覆盖旧文件,但是中间部分是我所苦的.正在编辑的xml文件是:
Ok i'm trying to write a batch to "grab" a serial number for the user. The serial number comes from another system and we have to periodically manually input a serial number. I would like the user to run the batch file which would add 1 and replace the old number and then echo "Your serial number is ##" where ## is the new number. I'm pretty sure i can work out the Echo part and the writing to a temp file and overwriting the old file but its the middle part that im struggling with. The xml file is that is being edited is:
<?xml version="1.0"?>
<BatchSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SerialFormat>{0:yyMMdd}{1:00}</SerialFormat>
<LastSerialDate>2020-05-27T00:00:00-05:00</LastSerialDate>
<LastSerialNumber>10</LastSerialNumber>
</BatchSettings>
我需要更改的数字是"lastserialnumber" < LastSerialNumber> 10</LastSerialNumber>
之间的##.我尝试了几种不同的方法,第一个方法很接近,但绝对没有雪茄.
the number i need to change is the ## between "lastserialnumber" <LastSerialNumber>10</LastSerialNumber>
.
I've tried a few different approaches and this first one is close but definitely no cigar.
@echo off
set /a "replace=10"
set /a "replaced=replace+1"
set "source=Test.txt"
set "target=Test2.txt"
setlocal enableDelayedExpansion
(
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %source%') do (
set "line=%%b"
if defined line set "line=!line:%replace%=%replaced%!"
echo(!line!
)
) > %target%
endlocal
一个问题是数字并不总是为"10",我也不想替换文件中所有其他"10"实例.
The problem with that one is the number won't always be "10" and I also don't want to replace all the other instances of "10" in the file.
我也尝试过
@Echo Off
Set "SrcFile=Test.txt"
Set "OutFile=Test2.txt"
@echo off
setlocal enableextensions disabledelayedexpansion
(for /f "delims=" %%a in (%SrcFile%
) do for /f "tokens=2 delims=1234567890 " %%b in ("%%~a"
) do if not "%%b"==":" (
echo(%%~a
) else for /f "tokens=1 delims= <LastSerialNumber>:" %%c in ("%%~a"
) do (
set /a "n=%%c+1"
setlocal enabledelayedexpansion
echo(<LastSerialNumber>!n!:
endlocal
)) > %OutFile%
但是不会增加数字并切断剩余的文本.而这只是一团糟,根本不起作用.
but that doesn't increment the number and cuts off the remaining text. and this one is just a hot mess that doesn't work at all.
@Echo Off
Set "SrcFile=Test.txt"
Set "OutFile=Test2.txt"
setlocal EnableDelayedExpansion
(for /F "delims=" %%a in ('findstr /I /L "<LastSerialNumber>" %SrcFile%') do (
set "line=%%a
set "line=!line:*<LastSerialNumber>=!"
for /F "delims=<" %%b in ("!line!") do echo %%b+1
)) > %oldnumber%
set "newnumber=%oldnumber%+1"
(
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" %SrcFile%') do (
set "line=%%b"
if defined line set "line=!line:%oldnumber%=%newnumber%!"
echo(!line!
)
) > %OutFile%
endlocal
推荐答案
首先,让我们快速了解您的尝试:
At first let us take a quick look at your attempts:
- 失败,因为它仅搜索
10
,并且根本不考虑< LastSerialNumber>
标记. - 失败,因为您尝试为/F 的
的
delims =
选项定义了一个字符串,尽管它实际上定义了一组字符. - 失败,因为您尝试将(
>
)重定向到无法工作的变量,因此只能重定向到文件.此外,您尝试进行算术运算(+1
),但这仅在使用set/A
时有效.最后一节遇到了与方法1相同的问题.
- fails as it searches just for
10
and does not regard the<LastSerialNumber>
tags at all. - fails because you try to define a string for the
delims=
option offor /F
although it actually defines a set of characters. - fails because you try to redirect (
>
) into a variable, which cannot work, you can only redirect to files. Furthermore, you attempt to do arithmetics (+1
) but this only works when usingset /A
. And the last section has got the same problem as approach 1.
这是您可以做到的方式:
Here is a way you could do it:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (input file; `%~1` is the first argument)
set "_TAG=<LastSerialNumber>" & rem // (opening tag)
rem // Loop over all (non-empty) lines of the input file:
for /F "usebackq delims=" %%L in ("%_FILE%") do (
rem // Store current line string:
set "LINE=%%L"
rem // Toggle delayed expansion to avoid troubles with `!`:
setlocal EnableDelayedExpansion
rem // Split off opening tag from line string:
set "TEST=!LINE:*%_TAG%=!"
rem // Check whether opening tag has been found:
if not "!TEST!"=="!LINE!" (
rem // Opening tag found, hence split off closing tag:
for /F "tokens=1* eol=< delims=< " %%S in ("!TEST!") do (
rem // Get extracted number and increment it:
set /A "NUM=%%S+1"
rem // Return rebuild line with incremented number:
echo( !_TAG!!NUM!^<%%T
)
) else (
rem // Opening tag not found, hence return original line:
echo(!LINE!
)
endlocal
)
endlocal
exit /B
鉴于该批处理文件名为 inc-serial.bat
,请使用输入XML文件作为命令行参数对其进行调用(例如,当前目录中的 test.xml
):
Given the batch file is called inc-serial.bat
, call it with the input XML file as a command line argument (say test.xml
in the current directory):
inc-serial.bat "test.xml"
要将输出写入另一个文件,请使用重定向(>
):
To write the output to another file use redirection (>
):
inc-serial.bat "test.xml" > "test_NEW.xml"
这篇关于批量替换并将1添加到xml文件中特定文本之间的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!