Saved Bookmarks
| 1. |
Solve : how to use expression in for loop? |
|
Answer» Hi Guys, I know the statement "set /A count = %count% + 1" is not correct but don't know how to fix it. set /A count=%count%+1 set /A count+=1 are equivalent but in a loop you need to USE delayed expansion (exclamation marks instead of percent signs) in the long form set /A count=!count!+1 (best to omit spaces) (google for zillions of explanations) Quote from: Salmon Trout on October 17, 2011, 08:22:20 AM set /A count=%count%+1 Set /A count=count+1 is also equivalent both inside and outside of a loop, delayed expansion not necessary for a Set /A command LINE.. A little demo: Code: [Select]echo off setlocal cls for /l %%1 in (1,1,20) do ( set /a count=count+1 set /a cntr=cntr+1 set /a number=count+cntr ) set /a number=number+100 echo Number=%number% Quote from: Dusty on October 19, 2011, 12:57:35 AM Set /A count=count+1 is also equivalent both inside and outside of a loop, delayed expansion not necessary for a Set /A command line.. Right. You can use both the long and short form set /a notations in a loop without delayed expansion, and the final value after the loop is exited will be correct, but delayed expansion is definitely necessary if you want to, er, expand the variable inside the loop. |
|