I have following code:
@echo off
SET ITER=0
for %%i in (%*) do (
SET ITER+=1
ECHO %ITER%
)
The output is (for three arguments):
0
0
0
Expected output:
1
2
3
Why can’t I access the updated variable in the for loop?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Expansion of variables with percents is done before a statement/block is executed.
So in your case the complete block is expanded before the
echo %ITER%is executed, to constantecho 0.The variable ITER itself is updated in the loop properly.
To avoid this, you could use the delayed expansion, this works like percent expansion but just in the moment of execution