Skip to main content

How to Combine Multiple Conditions (AND/OR Logic) in Batch Script

In scripting, a simple IF statement that checks one condition is useful, but often you need to make decisions based on multiple factors. For example, "run this command only if the user is 'Admin' AND the mode is 'Production'," or "process this file if its extension is '.log' OR '.txt'." Most programming languages provide AND and OR keywords for this, but batch scripting does not have them inside an IF statement.

This guide will teach you the standard structural patterns to simulate AND and OR logic in your batch scripts. You will learn how to use nested IF statements for AND logic and sequential IF statements with GOTO for OR logic, enabling you to build complex and intelligent decision-making into your automation.

The Challenge: No AND or OR Keywords in IF

The most important thing to know is that you cannot write a command like this: IF "%User%"=="Admin" AND "%Mode%"=="Prod" ( ECHO OK ) This is invalid syntax and will not work. To achieve this logic, we must structure our script in a specific way.

Method 1: Simulating AND Logic with Nested IF Statements

To check if Condition A AND Condition B are both true, you simply nest one IF statement inside another. The code in the inner block will only ever run if both the outer and inner conditions are met.

Pattern

IF condition1 (
IF condition2 (
REM ... Code here only runs if BOTH conditions are true ...
)
)

Example

This script will only grant access if the user is "Admin" AND the day is "Friday".

@ECHO OFF
SET "Username=Admin"
SET "DayOfWeek=Friday"

ECHO Checking access rights...

IF /I "%Username%"=="Admin" (
ECHO First condition met: User is Admin.
IF /I "%DayOfWeek%"=="Friday" (
ECHO Second condition met: Day is Friday.
ECHO [SUCCESS] Full access granted.
)
)

If either of the conditions were false, the inner block would never be reached.

Method 2: Simulating OR Logic with Sequential IF Statements

To check if Condition A OR Condition B is true, you can use a series of IF statements that all jump to the same success label using GOTO. If any one of the conditions is true, the script jumps to the success block; if all of them fail, the script continues to the failure block.

Pattern

IF condition1 GOTO :Success
IF condition2 GOTO :Success
REM ... Add more OR conditions as needed ...

REM --- Failure Block ---
ECHO None of the conditions were met.
GOTO :End

:Success
REM ... Code here runs if ANY of the conditions were true ...

:End

Example

This script will process a file if its extension is .log OR .txt.

@ECHO OFF
SET "FileName=data.txt"
FOR %%F IN ("%FileName%") DO SET "FileExt=%%~xF"

IF /I "%FileExt%"==".log" GOTO :ProcessFile
IF /I "%FileExt%"==".txt" GOTO :ProcessFile

ECHO File type is not supported. Skipping.
GOTO :End

:ProcessFile
ECHO File type is valid. Processing "%FileName%"...

:End

A Note on Conditional Execution Operators (&& and ||)

Batch scripting has && and || operators, but they do not work inside an IF statement. They are used to chain commands together.

  • commandA && commandB: Run command A, and if it succeeds (sets ERRORLEVEL 0), then run command B. (Logical AND)
  • commandA || commandB: Run command A, and if it fails (sets a non-zero ERRORLEVEL), then run command B. (Logical OR)

Example

REM Check if a file exists AND then echo a message.
DIR "myfile.txt" > NUL && ECHO File "myfile.txt" exists.

This is a powerful shortcut for simple command chaining but is not a replacement for the IF statement structures when comparing multiple variables.

Common Pitfalls and How to Solve Them

  • Trying to use AND/OR: The most common mistake is trying to write IF ... AND .... Solution: Use the nested IF pattern for AND, and the sequential IF with GOTO pattern for OR.
  • Incorrect ELSE with Nested IFs: An ELSE clause always pairs with the nearest preceding IF. This can get confusing in deeply nested structures.
    IF condition1 (
    IF condition2 (ECHO A and B) ELSE (ECHO A and not B)
    ) ELSE (
    ECHO Not A
    )
  • "Spaghetti Code" with GOTO: Overusing GOTO for OR logic can make your script hard to read. Solution: Keep the logic clean. Group all your IF checks together, followed immediately by the failure block, and then the success block.

Practical Example: A User Access Control Script

This script demonstrates both AND and OR logic to determine a user's access level.

@ECHO OFF
SETLOCAL
SET "Username=Editor"
SET "SecurityLevel=2"

ECHO --- Access Control Check ---
ECHO User: %Username%, Security Level: %SecurityLevel%
ECHO.

REM --- Admin Check (User must be "Admin" AND level must be > 4) ---
IF /I "%Username%"=="Admin" (
IF %SecurityLevel% GTR 4 (
ECHO [ACCESS GRANTED] Welcome, System Administrator.
GOTO :End
)
)

REM --- Editor Check (User can be "Editor" OR "Writer") ---
IF /I "%Username%"=="Editor" GOTO :EditorAccess
IF /I "%Username%"=="Writer" GOTO :EditorAccess

ECHO [ACCESS DENIED] User role not recognized or insufficient privileges.
GOTO :End

:EditorAccess
ECHO [ACCESS GRANTED] Welcome, Content Team Member.

:End
ENDLOCAL

Conclusion

While batch scripting lacks explicit AND and OR keywords within its IF statements, the same logic can be easily and reliably constructed using specific patterns.

  • For AND logic, use nested IF statements. The inner code block will only execute if all outer conditions are met.
  • For OR logic, use a series of sequential IF statements that all use GOTO to jump to a single success block.
  • The && and || operators are for conditional command execution, not for combining conditions inside an IF statement.

By mastering these fundamental structures, you can build scripts that can handle complex, multi-faceted decisions.