HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to MS-DOS BatchVariablesConditions (IF)Loops (FOR)File OperationsParameters and Subroutines
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔀 Condition
🔁 Loop
⚙️ Commands & Flow
MS-DOS›Conditions (IF)

Conditions (IF)

Comparison with IF, file existence check, and use of ELSE.

Simple enough for a 5-year-old to understand

Compare with IF

Example code
1
set name=Alex
2
if "%name%"=="Alex" (
Checks if they are equal.
ConditionChecks if they are equal.
3
echo Familiar someone
If the condition is true.
Output / InputIf the condition is true.
4
) else (
5
echo Stranger
If the condition is false.
Output / InputIf the condition is false.
6
)
ConditionOutput / Input
💡 Hint: Putting the value in quotes prevents errors with empty values.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.How is the equality of two texts checked in Batch?

About this topic

Conditions in batch files are written with a single command and have three basic forms: text comparison, file existence testing, and error level testing. Each uses a different syntax.

In text comparison, putting quotes around both sides is almost mandatory. When a variable comes in empty, comparison without quotes breaks the line; the same issue occurs when the value contains spaces. Quotes solve both cases at once.

The error level test has a classic trap: the old format means "greater than or equal to" rather than "equal to this value". Therefore, checking for zero always yields true, and many scripts have faulty success checks.

What you will learn

  • Text comparison
  • The necessity of using quotes
  • File existence testing
  • Error level behavior
  • Chained branches

Who is this for?

  • Those who write error checks in their scripts
  • Those who get errors with empty variables
  • Those who check exit codes

When to use?

To test whether a command was successful, it must be checked using comparison operators for error levels; the old format is misleading.

Performing an existence check before file operations prevents scripts from failing midway.

Common mistakes

Wrong

@echo off
dir yokdosya.txt
if errorlevel 0 echo Successful

The old error level format means "the given value or greater"; even if the command fails and returns a code greater than zero, the condition evaluates to true. For a success check, one must write equality to zero using a comparison operator. This trap leads to silent incorrect checks in numerous scripts.

Correct

@echo off
dir yokdosya.txt
if %errorlevel% equ 0 (
  echo Successful
) else (
  echo Failed
)

Wrong

@echo off
set ad=
if %ad%==Ahmet (
  echo matched
)

Because the variable is empty, the comparison loses its operand and a syntax error occurs. The possibility of an empty value always exists; enclosing both sides of the comparison in quotes preserves the line structure and simply results in a non-match instead of an error.

Correct

@echo off
set ad=
if "%ad%"=="Ahmet" (
  echo matched
)

Wrong

@echo off
if exist veri.txt (
  echo exists
)
else (
  echo does not exist
)

The chained branch must begin on the line containing the closing parenthesis; if written on a separate line, the command processor fails to recognize it and throws an error. In batch files, the block structure is strict, and line-ending positions carry meaning.

Correct

@echo off
if exist veri.txt (
  echo exists
) else (
  echo does not exist
)

Frequently asked questions

How to perform a numerical comparison?

Letter-abbreviated operators are used: forms for equal, not equal, less than, greater than, along with their equality-combined versions. Both sides must be numerical; if one is text, the comparison is done as text and the result becomes misleading.

How do I ignore letter case?

A case-insensitive comparison option is added to the condition command. Using this option when testing user input allows accepting both uppercase and lowercase spellings of the same answer together.

How do I test whether a variable is defined?

The condition command has a defined format and works directly with the variable name without using percentage signs. It is the right tool when you need to distinguish between empty text and being undefined.

Summary

Both sides of the comparison are enclosed in quotes.

The old error level format means "or greater".

The success check is written with a comparison operator.

The chained branch starts on the line of the closing parenthesis.

File existence is tested with a separate format.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code