【发布时间】:2017-07-27 21:55:42
【问题描述】:
代码是不言自明的。我已经尝试了注释行中的命令,结果相同。最后几行是增量分配和证据启用的测试,elayedexpansion 作品。错误必须在 for 循环内。
@echo off
setlocal EnableDelayedExpansion
set count_k=5
for /l %%a in (1,1,5) do (
rem set a/ count_k+=1
rem set a/ "count_k+=1"
set a/ count_k=count_k+1
echo This is count_k per %count_k%
echo This is count_k exc !count_k!
)
echo After loop this is count_k %count_k%
set _var=first
set _var=second & echo %_var% !_var!
set count = 0
(
set /a count+=1
echo %count% fails
echo !count! works
)
这是上述批处理文件的输出:
This is count_k per 5
This is count_k exc 5
This is count_k per 5
This is count_k exc 5
This is count_k per 5
This is count_k exc 5
This is count_k per 5
This is count_k exc 5
This is count_k per 5
This is count_k exc 5
After loop this is count_k 5
first second
fails
1 works
【问题讨论】:
-
Cmd.exe,尽管看起来,与 DOS 无关。如果您正在编写新脚本,请不要使用批处理。使用 PowerShell。
-
不应该是
set /a而不是set a/吗? -
你正在设置一个名为
a/ count_k的变量;重新检查你的语法... -
忽略比尔_斯图尔特;他的噱头是对批处理文件标签提出问题并告诉 OP 使用 PowerShell。
标签: windows batch-file cmd