Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.)

like image 871
James Ko Avatar asked Feb 22 '16 02:02

James Ko


People also ask

What is the correct way to declare a Boolean data type?

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 .

Which is the correct way to declare a Boolean variable value to be true?

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.

How do you create a Boolean variable?

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.


1 Answers

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

like image 50
Magoo Avatar answered Sep 30 '22 00:09

Magoo