代碼不言自明。我已經在註釋行中嘗試了相同結果的命令。最後一行是增量分配和證據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