在特定行之后批添加xml行

在特定行之后批添加xml行,xml,batch-file,Xml,Batch File,我想创建一个批处理脚本,该脚本将执行以下操作: 查找特定的xml行,并添加特定的多行标记 例如:精确查找这组行: <tree_node> <rule_name>bla</rule_name> <rule_argument>bla</rule_argument> <acl_name>bla</acl_name> </tree_node> 布拉 布拉 布拉 然后在后面添加这组

我想创建一个批处理脚本,该脚本将执行以下操作: 查找特定的xml行,并添加特定的多行标记

例如:精确查找这组行:

<tree_node>
    <rule_name>bla</rule_name>
    <rule_argument>bla</rule_argument>
    <acl_name>bla</acl_name>
</tree_node>

布拉
布拉
布拉
然后在后面添加这组行:

<tree_node>
    <rule_name>TEST1</rule_name>
    <rule_argument>TEST2</rule_argument>
    <acl_name>TEST3</acl_name>
</tree_node>

测试1
测试2
测试3
另一种解决方案是在特定行号处插入标记。 有人能帮忙解决吗?我已经搜索过了,但没有找到任何关于我问题的东西

谢谢你的帮助

下面的批处理文件完全按照您的要求执行:

@echo off
setlocal EnableDelayedExpansion

rem Define CR variable containing a Carriage Return (0x0D)
for /F %%a in ('copy /Z "%~F0" NUL') do set "CR=%%a"

rem Define LF variable containing a Line Feed (0x0A)
set LF=^
%Do not remove%
%these lines%

rem Define the string to find
set "find=<tree_node>!CR!!LF!"
set "find=!find!    <rule_name>bla</rule_name>!CR!!LF!"
set "find=!find!    <rule_argument>bla</rule_argument>!CR!!LF!"
set "find=!find!    <acl_name>bla</acl_name>!CR!!LF!"
set "find=!find!</tree_node>"

rem Get the number of lines to copy
findstr /N /R /C:"!find!" input.txt > findstr.tmp
for /F "delims=:" %%a in (findstr.tmp) do set /A lines=%%a+4
del findstr.tmp

rem Read from input file
< input.txt (

   rem Copy the appropriate number of lines
   for /L %%i in (1,1,%lines%) do (
      set /P "line="
      echo !line!
   )

   rem Add the new lines
   echo ^<tree_node^>
   echo     ^<rule_name^>TEST1^</rule_name^>
   echo     ^<rule_argument^>TEST2^</rule_argument^>
   echo     ^<acl_name^>TEST3^</acl_name^>
   echo ^</tree_node^>

   rem Copy the rest of lines
   findstr "^"

rem Write to output file
) > output.txt

move /Y output.txt input.txt

相关dbenham的回答:为什么不使用能够正确读写XML文件的脚本语言呢?VBScript、Jscript、Powershell。这在Powershell中是可能的-但是我不知道如何做。欢迎提供任何例子。谢谢感谢这个脚本,它可以工作,但只与短文本。当添加较长的文本时,我发现findstr的错误太长。有其他解决方案的帮助吗?比如说PowerShell?其思想是修改.xml文件,以便在找到特定标记后插入新标记(如上面的示例所示)。谢谢请参阅我答案中的编辑。。。我可以请你选择这个作为“最佳答案”,并投票表决吗?谢谢
rem Define the string to find
set "find=<tree_node>!CR!!LF!"
set "find=!find!    <rule_name>bla</rule_name>!CR!!LF!"
set "find=!find!.*!CR!!LF!"
set "find=!find!.*!CR!!LF!"
set "find=!find!</tree_node>"