Batchfile: What's the best way to declare and use a boolean variable? Batchfile: What's the best way to declare and use a boolean variable? windows windows

Batchfile: What's the best way to declare and use a boolean variable?


set "condition="

and

set "condition=y"

where y could be any string or numeric.

This allows if defined and if not defined both of which can be used within a block statement (a parenthesised sequence of statements) to interrogate the run-time status of the flag without needing enabledelayedexpansion


ie.

set "condition="if defined condition (echo true) else (echo false)set "condition=y"if defined condition (echo true) else (echo false)

The first will echo false, the second true


I'm sticking with my original answer for the time being:

set "condition=true":: Some code...if "%condition%" == "true" (    %= Do something... =%)

If anyone knows of a better way to do this, please answer this question and I'll gladly accept your answer.


I suppose another option would be to use "1==1" as a truth value.

Thus repeating the example:

set condition=1==1:: some codeif %condition% (    %= Do something... =%)

Of course it would be possible to set some variables to hold true and false values:

set true=1==1set false=1==0set condition=%true%:: some codeif %condition% (    %= Do something... =%)