Windows CMD批处理脚本-如何避免剪切标记“!";在循环 [英] Windows CMD Batch Script - how to avoid cutting the mark "!" in the loop

查看:115
本文介绍了Windows CMD批处理脚本-如何避免剪切标记“!";在循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有XML文件myConfig.xml.

I have XML file myConfig.xml.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<test1.test.com id="valueTest1"/>
<test2.test.com id="valueTest1"/>
<test3.test.com id="valueTest1"/>
<installpath>C:\Temp\TESTxyz</installpath>
<userInput>
<entry key="myPassword" value="Qwerty123!"/>
<entry key="myLogin" value="John"/>
</userInput>

我需要在CMD中的批处理脚本中更改值.

I need in CMD in batch script change value in .

@echo off
setlocal EnableDelayedExpansion
set newValueInstallpath="D:\Work"

(for /F "delims=" %%a in (myConfig.xml) do (
set "line=%%a"
set "newLine=!line:installpath>=!"
if "!newLine!" neq "!line!" (
    set "newLine=<installpath>%newValueInstallpath%</installpath>"
)
echo !newLine!
)) > NEW_myConfig.xml

输出-NEW_myConfig.xml

OUTPUT - NEW_myConfig.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<test1.test.com id="valueTest1"/>
<test2.test.com id="valueTest1"/>
<test3.test.com id="valueTest1"/>
<installpath>D:\Work</installpath>
<userInput>
<entry key="myPassword" value="Qwerty123"/>
<entry key="myLogin" value="John"/>
</userInput>

在installpath中的更改值正确更改为myPassword剪切字符!"中的BUT值.如何使它不切我的标记!"

Change value in installpath is correctly changed BUT value in myPassword cut character "!". How to make it not cut my mark "!"

推荐答案

即使在for元变量扩展之后,延迟扩展也是在执行之前发生的最后一件事.现在,当这样的for元变量包含带有感叹号的值时,它将被延迟扩展所消耗.解决方案是切换延迟扩展,以便仅在需要时才启用,否则禁用:

Delayed expansion is the last thing that happens prior to execution, even after expansion of for meta-variables. When now such a for meta-variable contains a value with an exclamation mark this is going to be consumed by delayed expansion. The solution is to toggle delayed expansion so that it is enabled only when it is needed and disabled otherwise:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "newValueInstallpath=D:\Work"

(for /F "usebackq delims=" %%a in ("myConfig.xml") do (
    set "line=%%a"
    setlocal EnableDelayedExpansion
    set "newLine=!line:installpath>=!"
    if "!newLine!" neq "!line!" (
        set "newLine=<installpath>!newValueInstallpath!</installpath>"
    )
    echo(!newLine!
    endlocal
)) > "NEW_myConfig.xml"

endlocal

这篇关于Windows CMD批处理脚本-如何避免剪切标记“!";在循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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