使用setlocal enableDelayedExpansion错误写入文件

时间:2013-09-03 15:32:24

标签: batch-file

我想要一个脚本用这个输出写一个文件:

rem ===[A]==========[B]==========[C] # =========[D]==========[E]==========[F] # =========[G]==========[H]==========[I] 
set "A1= " & set "B1= " & set "C1= " # & set "D1= " & set "E1= " & set "F1= " # & set "G1= " & set "H1=5" & set "I1= "

在这种情况下,我想使用EnableDelayedExpansion
我写了这个剧本:

setlocal EnableDelayedExpansion
set file="test.bat"
echo rem ===[A]==========[B]==========[C] # =========[D]==========[E]==========[F] # =========[G]==========[H]==========[I] >> %file%
echo set "A1= " ^^& set "B1= " ^^& set "C1= " # ^^& set "D1= " ^^& set "E1= " ^^& set "F1= " # ^^& set "G1= " ^^& set "H1=5" ^^& set "I1= " >> %file%

但输出只有一行:

rem ===[A]==========[B]==========[C] # =========[D]==========[E]==========[F] # =========[G]==========[H]==========[I]

如何使用EnableDelayedExpansion创建我想要的输出?

1 个答案:

答案 0 :(得分:0)

转义特殊字符时,请使用一个^而不是两个。将脚本中的最后一行更改为:

echo set "A1= " ^& set "B1= " ^& set "C1= " # ^& set "D1= " ^& set "E1= " ^& set "F1= " # ^& set "G1= " ^& set "H1=5" ^& set "I1= " >> %file%