Skip to main content

How to Perform a Case-Insensitive String Comparison in Batch Script

When a script needs to evaluate user input or text from a file, it's often desirable to ignore the case of the letters. A user might type "Y", "y", or "yes", but your script should understand that they all mean the same thing. By default, the batch IF statement performs a strict, case-sensitive comparison, which can make your scripts feel brittle and unforgiving.

Fortunately, the IF command has a simple, built-in switch that makes this task trivial. This guide will show you how to use the /I switch to perform case-insensitive string comparisons, making your scripts more robust and user-friendly.

The Default Behavior: Case-Sensitive Comparison​

Without any special switches, the IF string1==string2 command performs an exact, literal comparison. The case of the characters must match perfectly for the condition to be true.

Example of the Problem​

This script asks for user input but only accepts an uppercase "YES".

@ECHO OFF
SET /P "Confirm=Do you want to proceed? (YES/NO): "

IF "%Confirm%"=="YES" (
ECHO You chose to proceed.
) ELSE (
ECHO You did not confirm.
)

If the user types "yes" or "Yes", the script will fail to recognize their confirmation.

Do you want to proceed? (YES/NO): yes
You did not confirm.

The Core Solution: The /I Switch​

The solution is to add the /I switch to the IF command. This switch makes the string comparison that follows it Insensitive to case.

Syntax: IF /I string1==string2 (command)

When this switch is used, the command processor treats lowercase letters and their uppercase counterparts as identical for the purpose of the comparison.

Basic Example: A Simple Case-Insensitive Check​

Let's fix the previous example by adding the /I switch.

@ECHO OFF
SET /P "Confirm=Do you want to proceed? (YES/NO): "

REM The /I switch is added right after the IF command.
IF /I "%Confirm%"=="YES" (
ECHO You chose to proceed.
) ELSE (
ECHO You did not confirm.
)

Now, the script works as expected, regardless of how the user types their answer.

Do you want to proceed? (YES/NO): yes
You chose to proceed.

A Note on User Input: CHOICE vs. SET /P​

While SET /P with an IF /I check is a great way to handle string input, for simple single-key confirmations (like Y/N), the CHOICE command is often a better tool. The CHOICE command is inherently case-insensitive by default.

CHOICE Example​

CHOICE /C YN /M "Are you sure?"
IF ERRORLEVEL 2 ECHO You chose NO.
IF ERRORLEVEL 1 ECHO You chose YES.

In this example, CHOICE will accept either "y" or "Y" for the first option without needing an /I switch. It's a more streamlined way to handle single-character input.

Common Pitfalls and How to Solve Them​

The main pitfall is forgetting that even with the /I switch, other syntax rules for the IF statement still apply.

  • Quoting is Still Mandatory: The /I switch does not remove the need to quote your variables. If a variable is empty and not quoted, the IF statement will still result in a syntax error.
    REM This will FAIL if %UserInput% is empty.
    IF /I %UserInput%==YES (ECHO OK)

    REM This is the CORRECT, robust syntax.
    IF /I "%UserInput%"=="YES" (ECHO OK)
  • It only affects ==: The /I switch is only for string comparisons. It has no effect on other IF conditions like IF EXIST or IF DEFINED.

Practical Example: A Robust User Confirmation (Y/N/C)​

This script demonstrates a robust prompt that requires the user to confirm an action. It uses IF /I to handle case-insensitivity and loops until it receives a valid answer.

@ECHO OFF
SETLOCAL
SET "TARGET_FOLDER=C:\JunkData"

ECHO [WARNING] This script will permanently delete the folder:
ECHO "%TARGET_FOLDER%"
ECHO.

:Prompt
SET "UserChoice="
SET /P "UserChoice=Are you sure you wish to continue? (Yes/No/Cancel): "

IF /I "%UserChoice%"=="Yes" GOTO :Delete
IF /I "%UserChoice%"=="Y" GOTO :Delete

IF /I "%UserChoice%"=="No" GOTO :Abort
IF /I "%UserChoice%"=="N" GOTO :Abort

IF /I "%UserChoice%"=="Cancel" GOTO :Abort
IF /I "%UserChoice%"=="C" GOTO :Abort

ECHO.
ECHO Invalid input. Please type Yes, No, or Cancel.
ECHO.
GOTO :Prompt

:Delete
ECHO.
ECHO Deleting folder... (Simulated)
REM RD /S /Q "%TARGET_FOLDER%"
ECHO Folder deleted.
GOTO :End

:Abort
ECHO.
ECHO Operation cancelled by user.

:End
ENDLOCAL

Conclusion​

Performing a case-insensitive comparison is a fundamental requirement for creating user-friendly and robust batch scripts.

  • By default, the IF string1==string2 comparison is case-sensitive.
  • The solution is to add the /I switch right after the IF command: IF /I "%string1%"=="%string2%".
  • This switch makes the comparison ignore the case of the letters, treating a and A as equal.
  • Remember that even with /I, you must still enclose your variables in double quotes to prevent syntax errors with empty or space-filled strings.

By making the /I switch a standard part of your toolkit, you can write more flexible and forgiving scripts that handle variable input correctly.