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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

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

#1 Post by alan_b » 24 Jan 2010 14:41

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 24 Jan 2010 15:34

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

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

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

#3 Post by alan_b » 24 Jan 2010 16:27

Thanks. It works nicely

Code: Select all

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

Regards
Alan

Post Reply