Reputation: 2393
I have a FOR loop in a batch file where in I print the counter value in echo statement. Sample code below:
SET cycles= (%%n+1) ****here n is a variable of value 1
for /l %%n in (1,1,%iterations%) do (
echo This is Iteration no: (%%n+%cycles%)
)
This is not working as it does not calculate and rather it says
For was not expected this time
I tried with (%%n+%%cycles) also but it does not work. What can I try next?
Upvotes: 0
Views: 816
Reputation: 82247
This simply fails, as you use parenthesis in your echo statement!
You have to escape them, as the closing bracket closes the FOR loop.
The same problem you get, when you expand the cycle
variable with percent, better use the delayed expansion, as the content will not be parsed any more.
setlocal EnableDelayedExpansion
SET cycles= (%%n+1) ****here n is a variable of value 1
set iterations=5
for /l %%n in (1,1,%iterations%) do (
echo This is Iteration no: (%%n+!cycles!^)
)
EDIT: A calculating version
setlocal EnableDelayedExpansion
SET cycles= (%%n+1)
set iterations=5
set "cyclesEscape=!cycles:)=^)!"
for /l %%n in (1,1,%iterations%) do (
set /a result=%cyclesEscape%
echo This is Iteration no: %%n Formula !cycles!=!result!
)
Upvotes: 2