How do I set and check a boolean variable in a for loop in a windows batch file? -
i'm pretty new windows batch files i'm sure question has obvious answer i've been digging around online hours , haven't been able find it. when run following batch file, why doesn't output "it worked!"?
@echo off set /a _converted="0" set _converted if _converted==0 (echo worked!) else (echo didn't work)
output:
_converted=0 didn't work
but (i'm not sure how reproduce), outputs:
environment variable _converted not defined
here's i've tried:
set
vsset /a
- quotes in different places (
set /a "_converted=0"
vs.set /a _converted="0"
) "if use of logical or modulus operators, need enclose expression string in quotes." - my problem in loop can reproduce issue outside of loop don't think delayed variable expansion or this question helps me though looked similar.
put %'s around variable before checking value. use delayed expansion if in loop
as @aacini mentioned in comment, following prints "it worked!":
@echo off set /a _converted="0" set _converted if %_converted%==0 (echo worked!) else (echo didn't work)
the problem original issue within loop , messed if
statement before moving simpler batch file try isolate problem. fix combine answer delayed variable expansion.
Comments
Post a Comment