windows - Batch counter not increasing -
code self explanatory. have tried commands in commented lines equal results. last lines test of incremental assignment , evidence enabledelayedexpansion works. fault must lie within loop.
@echo off setlocal enabledelayedexpansion set count_k=5 /l %%a in (1,1,5) ( rem set a/ count_k+=1 rem set a/ "count_k+=1" set a/ count_k=count_k+1 echo count_k per %count_k% echo count_k exc !count_k! ) echo after loop 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 output of above batch file:
this count_k per 5 count_k exc 5 count_k per 5 count_k exc 5 count_k per 5 count_k exc 5 count_k per 5 count_k exc 5 count_k per 5 count_k exc 5 after loop count_k 5 first second fails 1 works
i have never seen "a/" parameter "set" command before. sure not intended "/a", hosing results , code?
i hate hand out fish instead of teaching fish, similar trying do?
@echo off set count=0 echo before loop count is: %count% /l %%a in (1,1,5) ( @echo loop %%a set /a count=%count%+%%a ) echo outside loop count is: %count%
the output looks this:
before loop count is: 0 loop 1 loop 2 loop 3 loop 4 loop 5 outside loop count is: 5
by remarking out @echo off, looks this:
c:\users\loginid>rem @echo off c:\users\loginid>set count=0 c:\users\loginid>echo before loop count is: 0 before loop count is: 0 c:\users\loginid>for /l %a in (1 1 5) ( set /a count=0+%a ) c:\users\loginid>( set /a count=0+1 ) loop 1 c:\users\loginid>( set /a count=0+2 ) loop 2 c:\users\loginid>( set /a count=0+3 ) loop 3 c:\users\loginid>( set /a count=0+4 ) loop 4 c:\users\loginid>( set /a count=0+5 ) loop 5 c:\users\loginid>echo outside loop count is: 5 outside loop count is: 5
notice inside loop, reference %count% 0 (from before entering loop) no matter how many times reset inside loop. if make change inside loop
set /a count+=%%a
my final result echoing %count% result in 15. beyond this, not sure trying achieve.
Comments
Post a Comment