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

5

47

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

Hickson answered 22/2, 2016 at 2:29 Comment(5)
That's pretty much it. Variables in batch are either strings or 32-bit integers and nothing else.Sacramentarian
You could do if defined var, which gives the benefit of working within if, for, and other parenthetical code blocks without requiring delayed expansion. There's also conditional execution based on zero or non-zero errorlevel.Fogel
shouldn't it be "%condition%"=="true" ?Nardi
@svasa - if %condition% can be guaranteed to have a value, you don't need them; the quotes are just to avoid a syntax error if it's empty.Sacramentarian
@SomethingDark: All variables are strings; there are not numeric variables in Batch files. The arithmetic operations performed by set /A command just manage 32-bits integers, but that is not exactly the same thing...Ducky
H
26

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.

Hickson answered 28/2, 2016 at 16:27 Comment(2)
The answer of Magoo is better, as it also works inside of code blocksHydr
I imagine that the magoo one is faster to execute because it does not compare strings and it is the closest thing to what they do in another programming language with booleam. Example: C(Everything that is not 0 is true), batch(Everything with value is true)Ironclad
S
44
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

Skiagraph answered 22/2, 2016 at 4:0 Comment(1)
@AlexanderSchäl : More explanation. %condition% refers to the contents of the variable condition, so if defined %condition% is enterpreted as if defined y if condition has the value ySkiagraph
H
26

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.

Hickson answered 28/2, 2016 at 16:27 Comment(2)
The answer of Magoo is better, as it also works inside of code blocksHydr
I imagine that the magoo one is faster to execute because it does not compare strings and it is the closest thing to what they do in another programming language with booleam. Example: C(Everything that is not 0 is true), batch(Everything with value is true)Ironclad
P
8

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

Thus repeating the example:

set condition=1==1

:: some code

if %condition% (
    %= Do something... =%
)

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

set true=1==1
set false=1==0

set condition=%true%

:: some code

if %condition% (
    %= Do something... =%
)
Pastis answered 17/12, 2018 at 12:58 Comment(0)
E
0

This works for me. I suspect that trying to include quotes complicates things, but the quotes are probably not necessary when the value assigned does not have any whitespace.

set condition=true

:: Some code that may change the condition
set condition=false

if %condition%==true (
    :: Some work
) ELSE (
    :: Do something else
)
Epilepsy answered 29/9, 2022 at 14:54 Comment(0)
P
0

Be wary of using Boolean literals such as true=1 EQU 1 or true=1==1 (see timfoden's suggestion) when using delayed expansion:

SET "true=1 EQU 1"
IF ... (
    SET is_true=%true%
    IF !is_true! (...)
)                ^ ---> ( was unexpected at this time.

Print answered 18/6 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.