43

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

James Ko
  • 32,215
  • 30
  • 128
  • 239
  • 6
    That's pretty much it. Variables in batch are either strings or 32-bit integers and nothing else. – SomethingDark Feb 22 '16 at 02:35
  • 4
    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. – rojo Feb 22 '16 at 02:53
  • shouldn't it be "%condition%"=="true" ? – SomeDude Feb 22 '16 at 03:21
  • @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. – SomethingDark Feb 22 '16 at 03:41
  • @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... – Aacini Feb 22 '16 at 05:05

4 Answers4

42
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

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 6
    @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 `y` – Magoo Feb 23 '17 at 10:35
22

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.

James Ko
  • 32,215
  • 30
  • 128
  • 239
  • 5
    The answer of Magoo is better, as it also works inside of code blocks – jeb Feb 28 '16 at 19:09
  • 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) – Daniel Briceño Sep 01 '22 at 02:24
6

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... =%
)
timfoden
  • 403
  • 6
  • 4
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
)
MustardMan
  • 43
  • 1
  • 7