Skip to main content

How to Use Conditional Execution with || (OR) in Batch Script

In batch scripting, one of the most powerful and concise ways to handle errors is through conditional execution. Instead of writing a multi-line IF %ERRORLEVEL% ... block after every command, you can use a special operator to run a second command only if the first one fails. This is the job of the || (OR) operator.

This guide will teach you how to use the || operator to create simple and readable fallback and error-handling logic. You will also learn about its natural counterpart, the && (AND) operator, and how to combine them to build powerful, single-line commands.

What is Conditional Execution?

Conditional execution is a shorthand syntax that allows you to link two commands together. The execution of the second command is conditional upon the success or failure of the first command. It's a compact alternative to a full IF statement.

  • Success: A command is considered successful if it returns an %ERRORLEVEL% of 0.
  • Failure: A command has failed if it returns a non-zero %ERRORLEVEL%.

The "OR" Operator: || (Run on Failure)

The || operator is like a logical "OR". Think of it as saying: "Do the first thing, OR if that fails, do the second thing."

The command on the right side of || will only execute if the command on the left side fails (i.e., returns a non-zero exit code).

Syntax: CommandA || CommandB

Its Counterpart: The "AND" Operator && (Run on Success)

You cannot fully understand || without its opposite, the && operator. This is like a logical "AND". Think of it as saying: "Do the first thing, AND if that succeeds, do the second thing."

The command on the right side of && will only execute if the command on the left side succeeds (i.e., returns an exit code of 0).

Basic Example: A Simple Error Message

This is the most common use for ||. We try to perform an action, and if it fails, we immediately print an error message.

@ECHO OFF
ECHO Trying to find a file that does not exist...

REM The DIR command will fail because the file is not there.
REM This failure will trigger the ECHO command on the right.
DIR "non_existent_file.txt" || ECHO [ERROR] The file could not be found.

In the output, the DIR command first prints its own error message, and then our custom error message is triggered.

Trying to find a file that does not exist...
File Not Found
[ERROR] The file could not be found.
note

We will learn how to hide the "File Not Found" message in the Pitfalls section.

How || Works with %ERRORLEVEL%

The || and && operators are simply convenient shortcuts for checking the %ERRORLEVEL%.

  • CommandA || CommandB is functionally equivalent to:
    CommandA
    IF %ERRORLEVEL% NEQ 0 (CommandB)
  • CommandA && CommandB is functionally equivalent to:
    CommandA
    IF %ERRORLEVEL% EQU 0 (CommandB)

Using the operators is much cleaner and more readable for simple, one-line responses.

Chaining && and || for Complex Logic

You can combine both operators on a single line to create powerful IF-THEN-ELSE logic.

The Pattern: Command && (On Success Command) || (On Failure Command)

This works because if Command succeeds, the && part runs and the || part is skipped. If Command fails, the && part is skipped and the || part runs.

Example:

@ECHO OFF
ECHO Pinging a valid host...
PING -n 1 google.com > NUL && (ECHO Host is ONLINE) || (ECHO Host is OFFLINE)

ECHO.
ECHO Pinging an invalid host...
PING -n 1 fakehostname > NUL && (ECHO Host is ONLINE) || (ECHO Host is OFFLINE)

Output:

Pinging a valid host...
Host is ONLINE

Pinging an invalid host...
Host is OFFLINE

Common Pitfalls and How to Solve Them

Problem: Command Output Clutter

As seen in the first example, the failing command (DIR) printed its own error message, which can be messy.

Solution: Redirect Error Output to NUL

To create a clean, custom error message, you must suppress the original command's output. You do this by redirecting its standard output (>) and standard error (2>) to the NUL device.

DIR "non_existent_file.txt" > NUL 2> NUL || ECHO [ERROR] The file could not be found.

Now, the only thing you will see is your clean, custom error message.

Problem: The First Command Succeeded, So Nothing Happened

This isn't an error, but a misunderstanding of the logic. A user might write DIR file.txt || ECHO It worked! and be confused when nothing is printed.

Solution: This is the correct behavior. || only runs on failure. If you want to run a command on success, you must use the && operator.

Practical Example: A "Ping or Fail" Script

This script checks for a network connection to a critical server. If the PING command fails, it logs an error message with a timestamp and exits.

@ECHO OFF
SETLOCAL
SET "CRITICAL_SERVER=db-prod-01"
SET "LOG_FILE=network_monitor.log"

ECHO --- Checking connection to %CRITICAL_SERVER% ---

REM Ping the server once. Suppress all output.
PING -n 1 "%CRITICAL_SERVER%" > NUL 2> NUL || (
ECHO [FAILURE] The server is offline!
ECHO %DATE% %TIME% - CRITICAL: Could not connect to %CRITICAL_SERVER%. >> "%LOG_FILE%"
EXIT /B 1
)

ECHO [SUCCESS] The server is online.
EXIT /B 0
note

This is an incredibly concise and powerful way to build a prerequisite check into a script.

Conclusion

The conditional execution operators are essential tools for writing clean and efficient batch scripts. They provide a powerful shorthand for IF %ERRORLEVEL% checks, allowing you to handle success and failure conditions on a single, readable line.

Key takeaways:

  • || (OR): Executes the next command only if the previous command fails (non-zero %ERRORLEVEL%). Perfect for error messages and fallback logic.
  • && (AND): Executes the next command only if the previous command succeeds (zero %ERRORLEVEL%). Perfect for sequential tasks.
  • Combine them for powerful IF-THEN-ELSE logic: Do-Something && (On-Success) || (On-Failure).
  • Use redirection (> NUL 2> NUL) on the first command to suppress its native output and provide your own clean messages.