<http://www.netikka.net/tsneti/info/tscmd120.htm>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Wed 21-Apr-2010 21:26:22

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



120} How do I use AND/OR/XOR/NOT operators in an IF statement?

  @echo off & setlocal enableextensions
  if [%2]==[] (
    echo Usage %~dpn0 var1 var2
    goto :EOF)
  set var1=%~1
  set var2=%~2
  ::
  if "%var1%"=="Timo" if "%var2%"=="Salmi" echo Timo AND Salmi
  ::
  set or_=
  if "%var1%"=="Timo" set or_=true
  if "%var2%"=="Salmi" set or_=true
  if defined or_ echo Timo OR Salmi
  ::
  set xor_=
  if "%var1%"=="Timo" set xor_=true
  if "%var2%"=="Salmi" set xor_=true
  if "%var1%"=="Timo" if "%var2%"=="Salmi" set xor_=
  if defined xor_ echo Timo XOR Salmi
  ::
  if not "%var1%"=="Timo" echo Not Timo
  if not "%var2%"=="Salmi" echo Not Salmi
  ::
  endlocal & goto :EOF

The output could be e.g.
  C:\_D\TEST>cmdfaq Timo Salmi
  Timo AND Salmi
  Timo OR Salmi

  C:\_D\TEST>cmdfaq Timo testing
  Timo OR Salmi
  Timo XOR Salmi
  Not Salmi

  C:\_D\TEST>cmdfaq Still testing
  Not Timo
  Not Salmi

Alternatively, the OR test could be written as
  set or_=true
  if not "%var1%"=="Timo" if not "%var2%"=="Salmi" set or_=
  if defined or_ (echo Timo OR Salmi) else (echo Neither)

Yet another way of testing an OR condition
  @echo off & setlocal enableextensions
  if "%~1"=="" (
    echo Usage %~dpn0 var1
    goto :EOF)
  ::
  echo %~1|findstr /c:"Timo" /c:"Salmi">nul&&echo Timo OR Salmi||echo Neither
  endlocal & goto :EOF

The output might be
  C:\_D\TEST>cmdfaq Timo
  Timo OR Salmi
or
  C:\_D\TEST>cmdfaq Foo
  Neither

Consider a variation of the task. If you have two variables (var1 and var2) test whether either (or both) have the value "Timo".
  @echo off & setlocal enableextensions
  if [%2]==[] (
    echo Usage %~dpn0 var1 var2
    goto :EOF)
  set var1=%~1
  set var2=%~2
  ::
  set or_=
  for %%a in ("%var1%" "%var2%") do if %%a=="Timo" set or_=true
  if defined or_ (echo Timo was found) else (echo Timo was not found)
  endlocal & goto :EOF

The output might be
  C:\_D\TEST>cmdfaq Timo Salmi
  Timo was found

Also see item #102 "How can I extract the individual bits from a decimal byte?"