无法将包含多个特殊字符的字符串传递给批处理脚本中的变量



在batchscript中,我试图捕获包含多个特殊字符的字符串

& lt;,比;、";Text", =,/, 等

在变量中,但我无法使用不同的转义字符

使其工作

, ", ^等

在不同的配置中,单个封闭,单独转义每个特殊字符,转义字符串部分不成功。

编辑:根据Squashmans的评论,我尝试使用enableddelayeexpansion,但这并没有解决问题

的例子:

setlocal enabledelayedexpansion
set "var=<ExampleString Id="63fc47e6-fafb-48f92-bd92-38ed9aa4a765" Name="The Name of this item." AnotherItem="Name of this item." AnotherID="AAA-123" Action="GO"><ItemOne><Item Id="*"/></ItemOne><ItemTwo><Location1 Path="MydriveFolder1*"/><Locaton2 Path="C:Program Files (x86)*"/></ItemTwo></ExampleString>"
ECHO %var%

它仍然返回错误:

& lt;

根据Squashmans使用enableddelayeexpand的注释,正确使用变量语法和下一个注释指出实际上使用!var!而不是%var%来延迟扩展,它工作得很好。

解决方案的例子:

setlocal enabledelayedexpansion
set "var=<ExampleString Id="63fc47e6-fafb-48f92-bd92-38ed9aa4a765" Name="The Name of this item." AnotherItem="Name of this item." AnotherID="AAA-123" Action="GO"><ItemOne><Item Id="*"/></ItemOne><ItemTwo><Location1 Path="MydriveFolder1*"/><Locaton2 Path="C:Program Files (x86)*"/></ItemTwo></ExampleString>"
ECHO !var!

使用setlocal enabledelayedexpansion,正确的变量语法set "var=content"和实际延迟!var!代替%var%展开

最新更新