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

What is the best way to declare and use a boolean variable in batch files? Here is what I am 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? (For example, in Bash, you can simply make if $condition , since true and false are native commands.)

+9
source share
3 answers

I stick to my original answer:

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

If anyone knows how best to do this, answer this question and I will gladly accept your answer.

+7
source
 set "condition=" 

and

 set "condition=y" 

where y can be any string or number.

This allows if defined and if not defined , both of which can be used in a block statement (a sequence of statements enclosed in parentheses) to query the state of the runtime of a flag without the need for enabledelayedexpansion


t

 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

+11
source

I assume that another option would use "1 == 1" as the value of truth.

Thus, repeating the example:

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

Of course, you can set some variables to hold true and false :

 set true=1==1 set false=1==0 set condition=%true% :: some code if %condition% ( %= Do something... =% ) 
0
source

Source: https://habr.com/ru/post/1243548/


All Articles