Page 1 of 1

2 "IF / ELSE" decisions, 4 combinations - reduce to 2 result

Posted: 24 Jan 2010 14:41
by alan_b
I would like to simplify / reduce
IF %A%==4 (DEFAULT CODE) ELSE IF %B% NEQ 6 (DEFAULT CODE) ELSE (SPECIAL CODE)
What I have shown above as DEFAULT CODE could be multiple lines - and is written twice.
I am attracted to
http://www.dostips.com/DtCodeSnippets.p ... lExecution
Command1 && (CommandBlock2 & REM) || (CommandBlock3)
I assume that this would work as well
(Command1) && (SPECIAL CODE & REM) || (DEFAULT CODE)
What does not work for me is
(IF %A%==4 IF %B% NEQ 6) && (SPECIAL CODE & REM) || (DEFAULT CODE)

I am hoping that some-one knows a clever way of wrapping two conditionals as a (COMMAND1)

So far all I can think of is

Code: Select all

SET R=DEFAULT
IF %A%==4 IF %B% NEQ 6 SET R=SPECIAL
IF %R%==DEFAULT (DEFAULT CODE) ELSE (SPECIAL CODE)

Alan

Re: 2 "IF / ELSE" decisions, 4 combinations - reduce to 2 result

Posted: 24 Jan 2010 15:34
by aGerman
Hi Alan.

Maybe you could use regular expressions. For testing:

Code: Select all

@echo off &setlocal
set /p "A=value for A: "
set /p "B=value for B: "

echo %A%:%B%|findstr /r /c:"^4:[^6]$" >nul 2>&1 &&call :default ||call :special

pause
goto :eof

:default
echo DEFAULT
goto :eof

:special
echo SPECIAL
goto :eof


regards
aGerman

Re: 2 "IF / ELSE" decisions, 4 combinations - reduce to 2 result

Posted: 24 Jan 2010 16:27
by alan_b
Thanks. It works nicely

Code: Select all

(echo %A%:%B%|findstr /r /c:"^4:[^6]$" > NUL ) && (SPECIAL & REM) || (DEFAULT

Regards
Alan