If you’re one of the many batch script programmers struggling with how to do string comparison in batch file, you’ve come to the correct place and should keep reading đź“‘ to find the answer.
We’ll explain batch files in this article and show you how to use them. Then we’ll give you a quick rundown of the string, and finally, we’ll describe string comparison in batch file. Furthermore, we will describe how to compare strings using the selection structure, for loop, and the find command.Â
So let’s take a deep dive into the article without further ado.
Table of Contents
What Is a Batch File and How Can We Use Them?
A plain text file containing a series of commands to be run by the command-line interpreter makes up batch scripting. Although it isn’t frequently used for programming, it isn’t frequently practised, and it isn’t trendy, yet its control and supremacy over the Windows environment must be addressed.
 A quick series of instructions sent into the Windows Command Prompt may perform and carry out almost any activity or action. We can run the batch script by entering commands in the command prompt or executing a batch file.
What Are String and String Interpolation?
A string is typically a sequence of characters in computer programming, either as a literal constant or as a variable. A string in DOS is an organized collection of characters, such as “Zeshan Afridi”. For example, if we want to store the string in a variable, then we write:
Code
myName="Zeshan Afridi" echo $myName
Output
Zeshan Afridi
In string interpolation, we create a new String value from a combination of constants, variables, literals, and expressions by combining their values inside a string variable.
When performing text interpolation in DOS programming, the set command can be used to align any literals, such as specified numeric variables, on a single line.Â
The following example demonstrates how to interpolate a string using numerical values and characters:
Code
@echo off SET a=Roll SET b=Number SET /A c = 22 SET d=%a% %b% is %c% echo %d% pause
Output
Roll Number is 22
Different Ways to do String Comparison in a Batch File
The following are the ways to compare strings in the batch file:
- Compare string in a Batch file using if structure.
- Compare string in a Batch file using the if-else structure.
- Compare string from a file using find.
- Compare string from a file using findstr.
- Compare string in a Batch file using for loop.
Method 1: Compare String in Batch File Using If Structure
The logic behind the If statement is such that if the condition becomes true, the If-block is executed; otherwise, the preceding statements in the program.. If two strings are compared in a batch file, the output will display “Strings are equal” on the console screen.Â
Now let’s understand this concept with the help of a simple example:
Code
@echo off rem Example of if Structure rem compare two string variables rem declare and initialize two string variables SET mystr1=Zeshan SET mystr2=Zeshan rem compare two string variables with the help of == operator if %mystr1%==%mystr2% echo "Two String variables are equal" rem pause the execution at the end of the file pause
Output
Two String variables are equal
We can also use relational operators(EQU, NEQ) with if Structure to compare two strings. For example
Code
@echo off rem Example of if Structure rem compare two string variables rem declare and initialize two string variables SET mystr1=Zeshan SET mystr2=Afridi rem compare two string variables with the help of the EQU operator if %mystr1% EQU %mystr2% echo "Two String variables are equal" rem compare two string variables with the help of NEQ if %mystr1% NEQ %mystr2% echo "Two String variables are Not equal" rem pause the execution at the end of the file pause
Output
Two String variables are Not Equal
Method 2: Compare String in Batch File Using If Else Structure
The If-else conditional structures work such that first, the If block will be executed only if the conditions return true otherwise, the else block will be executed.
Code
@echo off rem Example of if else Structure rem compare two string variables rem declare and initialize two string variables SET mystr1=Zeshan SET mystr2=Afridi rem compare two string variables with the help of if else if %mystr1% == %mystr2% (echo "Two String variables are equal") else (echo "Two String variables are Not equal") rem pause the execution at the end of the file pause
Output
Two String variables are Not Equal
Method 3: Compare String From Files Using Find
Find batch command looks for a string in input or files and outputs lines that match. Let’s understand this command with this example:
Text in myfile.txt is:
Zeshan Afridi Asim Amir Ali
Code
@echo off rem Example of find command rem Look for a string in the text file FIND "Asim" E:\myfile.txt pause
Output
Method 4: Compare String from File Using Findstr
You can search for a string in any plaintext file with the findstr command. You can search for a string with this command in a batch file and generate a statement based on the findings. In the example below, we compare the file strings with the given string and output message if the string is found in the file.
Code
@echo off rem Example of findstr command rem Look for a string in the text file findstr /m "Zeshan" E:\myfile.txt if %errorlevel%==0 ( echo String found in File ) pause
Output
String found in File
Method 5: Compare String in Batch File Using For Loop
To compare strings in a batch file, we can use a loop. Similar to if-else statements, loops are used to change the logic of a programme. Looping generally refers to repeatedly going through something until a condition is met. Only the direct implementation of the for loop is used in batch files. While and do while loops are not present like in other programming languages.
The General syntax of for loop:
FOR %%Loop_var IN set DO statements
Now let’s understand how the loop works with the help of a simple example:
Code
@echo off rem Example of for loop rem Look for a string in the text file for /f "usebackq tokens=* delims=" %%A in ("e:\myfile.txt") do ( echo %%A if "%%~A" == "Zeshan" (echo "String match is found") else (echo "String match is not found") ) pause
Output
Zeshan "String match is found" Afridi "String match is not found" Asim "String match is not found" Amir "String match is not found" Ali "String match is not found" Press any key to continue . . .
Conclusion
To conclude the article on how to make string comparison in Batch file, we covered five distinct approaches in the article. The selection structure(if and if-else) is the easiest approach to compare two strings in a batch file.Â
Additionally, we can use find, findstr, or a for loop to compare strings from files. A for loop is the best way to compare a string to each string in a text file and take action when a match is found.
If you found this article useful, please share it with your fellow programmers and let us know which method worked best for you in the comments section below ⬇️.
Happy programming! 🥳