1.

Solve : boolean functions?

Answer»

Is there a way to use boolean functions in the IF statement? Things like logical and, or, xor.

Something that would act, like this looks
Code: [Select]if %var%==x OR y (then...)Code: [Select]for %%a in (x y) do (
if '%var%' equ '%%a' (
commands
)
)Thanks for the help devcom.

If I am understanding your code correctly, both x and y will be compared against %var% separately. Problem is, this way, there is a true/false PRODUCED for both x and y. I need to logically say (X AND Y) or (X OR Y). That way I have only one true/false produced. I hope that makes sense. Perhaps I'm the one confused.Maybe you want something else.
In Boolean logic OR is inclusive, not exclusive.
Do you want to say either but not both? You use the XOR which is exclusive.
Do that make sense? Lets say A could be 0 or 1 and B could be 0 or 1. You want the case where A or B has a value f 1, but not both.
You would have something like like this
if A + B = 0 goto getouttahere
if A + B = 2 goto getouttahere
some commands
....
:getouttahere

Sorry that is not real BATCH code, but you get the idea.

An example:
Code: [Select]@echo off

set var=x

for %%x in (x y) do (
if "%var%"=="%%x" (
echo true
) else (
echo false
)
)

pause
The output of the example:
Code: [Select]true
false
Press any key to continue . . .
X and Y are processed separately and compared against "%var%" separately, yielding one true and one false. I am needing to have them processed as one logical statement which would yield only one true/false. Perhaps this is just not POSSIBLE with a batch file.Maybe these will help

1. from set /? (My asterisks)

Code: [Select]The /A switch specifies that the string to the right of the equal sign
is a numerical expression that is evaluated. The expression evaluator
is pretty simple and supports the following operations, in decreasing
order of precedence:

() - grouping
! ~ - - unary operators
* / % - arithmetic operators
+ - - arithmetic operators
<< >> - logical shift
& - bitwise and
^ - bitwise exclusive or
| - bitwise or
= *= /= %= += -= - assignment
&= ^= |= <<= >>=
, - expression separator

*** If you use any of the logical or modulus operators, you will need to
enclose the expression string in quotes.***

Any non-numeric strings in the
expression are treated as environment variable NAMES whose values are
converted to numbers before using them. If an environment variable name
is specified but is not defined in the current environment, then a value
of zero is used. This allows you to do arithmetic with environment
variable values without having to type all those % signs to get their
values.
2.

http://groups.google.com/group/alt.msdos.batch.nt/search?hl=en&group=alt.msdos.batch.nt&q=boolean&qt_g=Search+this+group

Thanks everyone.

@ Dias de verano

Thank you.
Through the link you provided, I was able to find this ftp://garbo.uwasa.fi/pc/link/tscmd.zip. Many good examples found there, including how to incorporate AND/OR/XOR/NOT into IF statements.

An example of logical OR the long drawn out batch file way:
IF "%var%" is something other than "a" or "b" or "", then ask user for valid input.
Code: [Select]: logical.or.loop
set logical.or=
if "%var%"=="a" (set logical.or=true)
if "%var%"=="b" (set logical.or=true)
if "%var%"=="" (set logical.or=true)
if not "%logical.or%"=="true" (
echo.
set var=
set /p var=Invalid input, please try again.
goto logical.or.loop
)



Discussion

No Comment Found