代码不言自明。我已经在注释行中尝试了相同结果的命令。最后一行是增量分配和证据enabledelayedexpansion工作的测试。该故障必须位于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。 –
希望这会有所帮助:https://stackoverflow.com/questions/21697199/how-to-increment-variable-under-dos –
不应该是'set/a'而不是'set a /'? – kichik