What's the best way to declare and use a boolean variable in Batch files? This is what I'm doing now:
set "condition=true" :: Some code that may change the condition if %condition% == true ( :: Some work )
Is there a better, more "formal" way to do this? (e.g. In Bash you can just do if $condition
since true
and false
are commands of their own.)
Boolean Types A boolean data type is declared with the bool keyword and can only take the values true or false . When the value is returned, true = 1 and false = 0 .
To declare a Boolean variable, we use the keyword bool. To initialize or assign a true or false value to a Boolean variable, we use the keywords true and false.
One of them is ' bool . ' The ' bool ' type can store only two values: true or false . To create a variable of type bool, do the same thing you did with int or string . First write the type name, ' bool ,' then the variable name and then, probably, the initial value of the 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With