String comparison in batch files
A string is an ordered collection of characters.
You can compare strings using conditional commands in batch files, namely, if, if-else, and for commands. Strings may contain spaces and special characters, which can cause errors in the batch file when executed normally.
Also, if the string contains double quotes, it may cause errors.
This tutorial will show you different ways to compare strings in batch files using if, if-else, and for commands.
Using if and if-else commands for string comparison in batch files
The if command performs conditional operations and logical comparisons between a set value and an expected value. It can check if a file exists, compare strings, and even check for errors.
Using the if command to compare strings
Let us take an example to check if the strings are equal or not. If they are equal, it will display the output using the echo command.
@echo off
SetLocal
set string1="Hello World"
set string2="Hello World"
if %string1% == %string2% (echo "Both the strings are equal")
cmd /k
Output:
Using if-else command to compare strings
@echo off
SetLocal
set string1="Hello World"
set string2="HELLO WORLD"
set string3="Hello World"
if %string1% == %string2% (echo string 1 and string 2 are equal echo the string is %string2%) else if %string1% == %string3% (echo string1 and string 3 are equal
echo the string is %string3%) else (echo all strings are different)
cmd /k
Output:
Notice:
- Do not put spaces between brackets.
- When strings or variables contain spaces or special characters, enclose them in double quotes.
With the if command, you can use /I
to do a case-insensitive string comparison and not run the command if the condition is false.
You can also use comparison operators such as EQU (equal to), NEQ (not equal to), LSS (less than), LEQ (less than or equal to), GTR (greater than or equal to), GEQ (greater than or equal to) to compare values. However, these operators cannot be used to compare strings.
Use if-else command to compare strings containing double quotes
If the string or variable contains double quotes, use the SetLocal command to enable delayed expansion. Use ! instead of ".
The same code looks like this:
@echo off
SetLocal EnableDelayedExpansion
set string1="Hello "World""
set string2=""HELLO WORLD""
set string3="Hello World"
if !string1! == !string2! (echo "string 1 and string 2 are equal") else if !string1! == !string3! (echo "string1 and string 3 are equal") else (echo "All strings are different")
cmd /k
Output:
Using for loop for string comparison in batch file
The for command is used to run a specified command for each file in a set of files.
Shows the syntax for the for command in a batch file.
for {%%|%}<variable> in (<set>) do <command> [<commandlineoptions>]
Let's take an example where we have to compare strings to display the Windows OS version:
@echo off
SetLocal EnableDelayedExpansion
for /f "usebackq tokens=1 delims=" %%I in ("C:\Users\Aastha Gas Harda\Desktop\testfile1.txt") do (
set string=%%I
echo %%I
if "%%~I" == "HelloWorld" (echo "match found") else (echo "match not found")
)
PAUSE
Output:
In the above example, a for loop is used to compare the string values. If the string value is equal to HelloWorld, it will display the output as a match found.
delims
specifies the delimiter set, and tokens specifies which tokens in each line will be passed to the for loop. Whenever we use quotes in a for loop, we must use usebackq.
So, we have discussed different ways to compare strings in batch files. In addition to comparing strings, these conditional commands can also be used to find specified values or text from a text file or a log file.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Remove double quotes from variable in batch file
Publish Date:2025/03/22 Views:177 Category:OPERATING SYSTEM
-
In batch files, variables containing multiple words or spaces must be placed in double quotes, but sometimes, we do not want to see these quotes in the output. These quotes can be removed from the variables in batch files. There are many wa
Batch file to loop through files in subdirectories
Publish Date:2025/03/22 Views:96 Category:OPERATING SYSTEM
-
This article explains how we can write a batch script to loop through files in subdirectories. We will take an example to explain the concept. Batch file to loop through files in subdirectories Assume we have the directory structure shown b
Batch file to remove X characters from a file name
Publish Date:2025/03/22 Views:153 Category:OPERATING SYSTEM
-
This article explains how we can remove specific characters from the filename of a file using a batch script. We will cover several methods below to rename files on Windows. File Explorer Renaming on Windows File Explorer offers the least f
Stop a running process from a batch file
Publish Date:2025/03/21 Views:201 Category:OPERATING SYSTEM
-
This article explains how to stop a running process from a batch file in Windows. We use Batch's taskkill command to terminate the running process. 请注意 , the command will be executed only if the specified process is open. Batch file t
Get the current batch file directory
Publish Date:2025/03/21 Views:125 Category:OPERATING SYSTEM
-
This article demonstrates how to determine the location of a batch file. Batch scripts are great for automation. Sometimes you may need to get the location of a batch file. This article will help you determine the working directory and batc
Running batch files remotely
Publish Date:2025/03/21 Views:176 Category:OPERATING SYSTEM
-
Sometimes we need to use a computer remotely from a different location. We can use some third-party software such as TeamViewer to do this. But we can execute batch files from a remote directory without the need for third-party software. In
Run the batch file as administrator
Publish Date:2025/03/20 Views:142 Category:OPERATING SYSTEM
-
Batch files consist of commands that are executed by the Command Prompt. Some of these commands cannot be run without administrator privileges; therefore, it is important to run batch files as an administrator. You can manually run a batch
在 Windows 中使用批处理文件删除文件夹及其内容
Publish Date:2024/03/15 Views:310 Category:编程语言
-
本教程教授如何使用 bat 文件删除文件夹及其内容。