Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Done DO i=1 to %hi, re-evaluate hi each time

May
12,845
164
Could DO i=1 to %hi be made to re-evaluate hi on each iteration? ... for example, so that
Code:
do i=1 to 10 ( echos %i & set /q /a hi-=1 )
would produce
Code:
12345

I had a use for it today.
 
How do the conditionals work in DO? I tried a simple test that doesn't work.
Code:
set hi=10
do while hi gt 0 (echos %i & set /q /a hi-=1)
 
I also tried it in a batch file and that doesn't work either.
Code:
set hi=1
DO while hi gt 0
    echos 1
    set hi=0
ENDDO
 
Well, apparently, if you leave off the %'s in front of the hi the script runs just fine but the variable is not evaluated. If you add in the %'s it does what is expected.
Code:
set hi=1 & do while %hi gt 0 (echo hi & set hi=0)
 
I imagine DO WHILE or DO UNTIL would do what you want. You'd have to handle initialization and incrementation yourself, though.
 
The conditional is very picky though.
This does not work:
Code:
DO WHILE %i != %hi (...)
And this code returns an error:
Code:
$ set done=0 & set hi=10 & set i=1 & do while %done==0 (echos %i & set /q /a i+=1 & set /q /a hi-=1 & set done=%@if[%i==%hi,1,0])
TCC: Unknown command "set"
 
My bad. Bad copy & paste. This works, but is ugly.
Code:
set done=0 & set hi=10 & set i=1 & do while %done==0 (echos %i & set /q /a i+=1 & set /q /a hi-=1 & if %i GE %hi (set done=1) else (set done=0)
)
 
Back
Top