Skip to main content

How to Use Conditional Execution with && (AND) in Batch Script

In batch scripting, you often want to run a command only if the previous one was successful. For example, you might want to copy a file only if you could successfully create the destination directory first. While you could write a multi-line IF ERRORLEVEL 0 block for this, batch scripting provides a much more concise and elegant shortcut: the conditional execution operator &&.

This guide will teach you how to use the && operator to chain commands together based on success. You will also learn about its counterpart, || (OR), and understand the critical difference between these operators and the IF statement, a common point of confusion for new scripters.

What is Conditional Execution? The Role of ERRORLEVEL

The foundation of conditional execution is the %ERRORLEVEL% variable. By convention, command-line programs in Windows return an exit code after they finish:

  • ERRORLEVEL 0: The command completed successfully.
  • ERRORLEVEL 1 (or higher): The command failed or encountered an error.

The && and || operators check this exit code automatically to decide whether to run the next command.

The && (AND) Operator: Run on Success

The && operator is the logical AND. It chains two commands together with the following rule:

Run Command A, AND if it succeeds (ERRORLEVEL 0), then run Command B.

If Command A fails, Command B is never executed.

Basic Example

Let's use the DIR command, which succeeds if a file is found and fails if it is not.

@ECHO OFF
ECHO --- Success Scenario ---
REM This DIR will succeed, so the ECHO command will run.
DIR "C:\Windows\System32\kernel32.dll" > NUL && ECHO Found the kernel32.dll file.

ECHO.
ECHO --- Failure Scenario ---
REM This DIR will fail, so the ECHO command will NOT run.
DIR "non_existent_file.txt" > NUL 2> NUL && ECHO This message will not be displayed.

Output:

--- Success Scenario ---
Found the kernel32.dll file.

--- Failure Scenario ---

The || (OR) Operator: Run on Failure

The || operator is the logical OR. It provides the opposite behavior:

Run Command A, OR if it fails (ERRORLEVEL is not 0), then run Command B.

If Command A succeeds, Command B is never executed. This is perfect for error handling.

Basic Example

Let's use the PING command to check for a server and print a message if it fails.

@ECHO OFF
ECHO Pinging a valid server...
REM This ping will succeed, so the ECHO command will NOT run.
PING -n 1 google.com > NUL || ECHO The server is down!

ECHO.
ECHO Pinging an invalid server...
REM This ping will fail, so the ECHO command WILL run.
PING -n 1 an_invalid_server_name > NUL || ECHO The server is down!

Output:

Pinging a valid server...

Pinging an invalid server...
The server is down!

Chaining Multiple Commands

You can chain these operators together to create powerful one-liners. The commands are evaluated from left to right.

This command chain will change to the D: drive, create a Logs directory if it doesn't exist, and then echo a success message. The chain will stop at the first point of failure.

@ECHO OFF
CD /D D:\ && MKDIR Logs 2>NUL && ECHO Successfully created the D:\Logs directory.

Critical Distinction: && vs. the IF Statement

This is the most important pitfall to understand. The && and || operators cannot be used inside an IF statement to combine variable comparisons.

Example of script with error (invalid syntax)

REM This will NOT work.
IF "%VAR1%"=="A" && "%VAR2%"=="B" (ECHO Both are correct.)

The && operator is for chaining commands, not for evaluating multiple conditions within an IF statement. To achieve that, you must use nested IF statements.

Corrected Script (with nested IF)

IF "%VAR1%"=="A" (
IF "%VAR2%"=="B" (
ECHO Both are correct.
)
)

Common Pitfalls and How to Solve Them

  • Commands That Don't Set ERRORLEVEL: Some commands, especially simple internal ones like ECHO or SET, don't reliably set ERRORLEVEL on their own, which can make them poor choices for the left side of a conditional operator.
  • Readability: While powerful, a very long chain of && and || can become difficult to read and debug. For complex logic, a full IF / ELSE block is often more maintainable.

Practical Example: A "Safe Copy" Script

This is a classic use case. The script first checks if the source file exists. Only if that check succeeds does it proceed to copy the file.

@ECHO OFF
SET "SOURCE_FILE=C:\Data\report.csv"
SET "DEST_FOLDER=E:\Backups"

ECHO --- Safe File Copy ---
ECHO Checking for source file...

REM 1. Use DIR to check for the file. It sets ERRORLEVEL 0 if found.
REM 2. Use && to chain the COPY command, which will only run if DIR succeeded.
DIR "%SOURCE_FILE%" > NUL 2> NUL && (
ECHO Source file found. Copying...
COPY "%SOURCE_FILE%" "%DEST_FOLDER%" > NUL
ECHO Copy complete.
) || (
ECHO [ERROR] Source file not found. Nothing was copied.
)

This example even chains an || at the end to provide a clean error-handling block.

Conclusion

The conditional execution operators && and || are powerful tools for creating concise and effective batch scripts. They are shortcuts that allow you to chain commands based on the success or failure of the preceding command.

Key takeaways:

  • commandA && commandB: Runs commandB only if commandA succeeds (ERRORLEVEL 0).
  • commandA || commandB: Runs commandB only if commandA fails (ERRORLEVEL is not 0).
  • These operators are for chaining commands, and they cannot be used inside an IF statement to combine variable comparisons.
  • They are perfect for simple, sequential logic, like checking for a file before acting on it.