Page 1 of 1
compare multiple strings in same line
Posted: 15 Nov 2010 10:57
by akshunj
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.
Re: compare multiple strings in same line
Posted: 15 Nov 2010 12:23
by jeb
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
Re: compare multiple strings in same line
Posted: 16 Nov 2010 02:49
by amel27
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%