Combine IF statetement with AND/OR

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Combine IF statetement with AND/OR

#1 Post by sambul35 » 16 Feb 2016 12:02

This Cmd Syntax reference page says, piping commands is possible in IF statements like this:

Code: Select all

IF (... | ...)

Does it actually work in Win 10 Cmd batches? Can you give a working code example?

Is there a similar way in a batch to process AND/OR multiple conditions inside one IF statement instead of repeating IF statements for each condition separately on a new line?


Related, why IF statements don't work when entered on the same line like this:

Code: Select all

IF %var%==s GOTO :test1 || IF %var%==t GOTO :test2


Is there a way to make code more compact and efficient in this case, if there're multiple IF statements?
Last edited by sambul35 on 17 Feb 2016 12:25, edited 1 time in total.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Combine IF statetement with AND /OR

#2 Post by ShadowThief » 16 Feb 2016 14:29

|| and && are used when you want to conditionally run a second command based on the first command; they have nothing to do with if statements.

command1&&command2 means that you run command2 if command1 returns an errorlevel of 0
command1||command2 means that you run command2 if command1 does not return an errorlevel of 0


And to answer your question, if you surround both if statements with parentheses, you can use a single & to join them.

Code: Select all

(if "%var%"=="s" (goto :test1))&(if "%var%"=="t" (goto :test2))

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: Combine IF statetement with AND/OR

#3 Post by sambul35 » 17 Feb 2016 14:29

Are you saying there's no way to combine single IF with AND/OR in a batch?

Can someone give a working example of a single IF (... | ...) piping statement?

Post Reply