Table of Contents
Comparing Strings in Batch File
Batch files are a powerful tool for automating tasks in Windows systems. One common operation performed in batch files is string comparison. String comparison is used to determine if two strings are equal or not. This is useful when you need to perform different actions based on the value of a string.
Creating batch files is simple and typically saved with a .bat
file extension we have already explained here. Now, let’s continue learning various methods to compare strings in the Batch file.
Use IF-ELSE
Statements
Use IF-ELSE
statements to check if two strings are equal in a batch file.
1 2 3 4 5 6 7 8 9 10 |
@echo off set string1="Hello" set string2="Hello" if %string1% == %string2% ( echo "Strings are equal." ) else ( echo "Strings are not equal." ) |
1 2 3 |
Strings are equal. |
You can use the IF-ELSE
statement to compare strings in batch files. The IF
statement checks if the strings are equal. The %
symbol is used in batch files to enclose variables.
When a variable is enclosed in %
symbols, the batch processor replaces it with the variable’s value, and a comparison is made using the ==
operator. If the IF
statement is True
/False
, the user is informed by displaying a message on the console that your given strings are equal/not equal.
If you have space or special characters in the String, you should always put String in double quotes.
Use EQU
Operator
Use the EQU
operator to check if two strings are equal in a batch file.
1 2 3 4 5 6 7 8 9 10 11 |
@echo off set string1="Hello" set string2="Hello" IF %string1% EQU %string2% ( echo "Strings are equal." ) ELSE ( echo "Strings are not equal." ) cmd /k |
1 2 3 |
Strings are equal |
The above code is similar to the previous one, except we used the EQU
operator instead of the ==
operator. The EQU
operator checks whether two strings are equal.
Use NEQ
Operator
Use the NEQ
operator to check if two strings are not equal in a batch file.
1 2 3 4 5 6 7 8 9 10 11 |
@echo off set string1="Hello" set string2="World" IF %string1% NEQ %string2% ( echo "The first string is not equal to the second string." ) ELSE ( echo "The first string is equal to the second string." ) cmd /k |
1 2 3 |
The first string is not equal to the second string. |
We used the NEQ
operator to check if two strings were unequal. If the IF
statement is True
/False
, the user is informed by displaying a message on the console that your given strings are not equal or equal.
Considering the above solutions, string comparison is a crucial task in many batch files, and various techniques are available to perform this task. The most commonly used technique is the IF
statement, which allows for direct comparison using the ==
operator.
The EQU
and NEQ
operators are also available to compare strings for equality and inequality, respectively. The batch file developers can perform string comparisons with ease, helping to streamline the execution of their batch files.
Comparison of String with double quotes in Batch File
If String contains double quotes, use !
rather than %
while comparing the String and also enable delayed expansion with SetLocal
command.
1 2 3 4 5 6 7 8 9 10 |
@echo off SetLocal EnableDelayedExpansion set str1="Hello from "Java2blog"" set str2="Hello from "Java2blog"" if !str1! == !str2! (echo "string 1 and string 2 are equal") else (echo "string 1 and string 2 are not equal") cmd /k |
1 2 3 |
"string 1 and string 2 are equal" |
That’s all about String comparison in Batch File.