compare multiple strings in same line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
akshunj
Posts: 1
Joined: 15 Nov 2010 10:49

compare multiple strings in same line

#1 Post by akshunj » 15 Nov 2010 10:57

Hi,

Can anyone provide some insight as to how I could compare multiple strings in the same command. For example:

if string1==string2 (or) string3 (or) string4 set var=X

I've tried using commas and other symbols to seperate the multiple strings, but haven't been able to make it work.

Thx.

jeb
Expert
Posts: 1043
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: compare multiple strings in same line

#2 Post by jeb » 15 Nov 2010 12:23

Hi akshunj,

there is a simple answer, bool expressions are not supported by batch files. :(

But you can use tricks.

Code: Select all

call :logic_or result "%string1%==%string2%"   "%string1%==%string3%" "%string1%==%string3%"
if %result%==1 set var=x
goto :eof

:logic_or <resultVar> expression1 [[expr2] ... expr-n]
:: Liefert in resultVar eine 1 wenn midestens eine Bedingung "Wahr" war
SETLOCAL
set "logic_or.result=0"
set "logic_or.resultVar=%~1"

:logic_or_loop
if "%~2"=="" goto :logic_or_end
if %~2 set "logic_or.result=1"
SHIFT
goto :logic_or_loop

:logic_or_end
(
  ENDLOCAL
  set "%logic_or.resultVar%=%logic_or.result%"
  goto :eof
)


hope it helps
jeb

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: compare multiple strings in same line

#3 Post by amel27 » 16 Nov 2010 02:49

akshunj wrote:how I could compare multiple strings in the same command.

Code: Select all

set "OR_Cmd=set var=X"
if not string1==string2 (if not string1==string3 (if string1==string4 %OR_Cmd%) else %OR_Cmd%) else %OR_Cmd%

Post Reply