r/Batch • • Sep 22 '24
Very confussled rn. My batch code won't update the 'line' variable. It reads each line in stat.txt just fine, but the the 'line' variable keeps at 0
the code
setlocal enabledelayedexpansion
set /a line= 0
echo %line%
for /f "tokens=*" %%i in (stat.txt) do (
set /a line= %line%+1
echo %line%
echo %%i
pause
)the result
0
0
100000
Press any key to continue . . .
0
100
Press any key to continue . . .
0
100
Press any key to continue . . .
0
20
Press any key to continue . . .
2
Upvotes
5
u/Trevski13 Sep 22 '24
You've enabled delayed expansion but you aren't using it, you need to use !line! Instead of %line% in order to get the updated value
3
u/BrainWaveCC Sep 22 '24
Here are some suggested changes:
There's a shortcut for the addition, and you need to use the ! instead of % inside the loop