2

I want to compare variable with multiple values with "OR" condition I batch script rather having multiple if logic.

@echo off

robocopy D:\SourceData E:\DestinationData

If %ErrorLevel% Equ 0 OR 1 OR 2 ( GoTo Success) Else ( GoTo Error)

:Success

Echo Robocopy completed successfully.
Pause
GoTo End

:Error

Echo Robo completed with some error/s.
Paude

:End
Exit

2 Answers 2

2

If you want to compare the error level with a certain list:

for %%a in (1 2 3 whatever) do (
  if %errorlevel% equ %%a (
     goto :Success
  )
)
goto :error
Sign up to request clarification or add additional context in comments.

Comments

1

ERRORLEVEL is rarely < 0, so you could use:

if %errorlevel% LEQ 2 ( GoTo Success) Else ( GoTo Error)

...and for safety just in case:

if %errorlevel% LEQ -1 ( GoTo Error)

If your values were not consecutive you could just duplicate the if lines without the else (not very good but would work)

If you know all the possible values you can just do

goto branch%ERRORLEVEL%

and define

:branch0
:branch1
:branch2

...and so on.

(this is more or less directly lifted from IF online help, I learned a lot with the /? switch of commands like that)

1 Comment

just to note: errorlevel can be negative. (although most programs don't use it, but there are some, that do)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.