Skip to main content

Batch Script Conditional Branching

Conditional Branching

Batch Script Conditional Branching is made using the if and if-else statements.

The programmer must specify one or more conditions to be evaluated or tested by the program, along with one or more instructions to be executed if the condition is true and, optionally, other instructions to be executed if the condition is false.

note

Note that the evaluation of the condition is "case-sensitive."

if Statement

The general working of if statement is that first a condition is evaluated in the if statement. If the condition is true, it then executes the statements.

if Syntax
if(condition) do_something

The following is an example of how if statements can be used:

@echo off 
set /A a=5
set /A b=10
set /A c=%a% + %b%
if %c%==15 echo "The value of variable c is 15"
if %c%==10 echo "The value of variable c is 10"
"The value of variable c is 15"

if-else Statement

The general operation of the if-else statement is that first a condition in the if statement is evaluated. If the condition is true, the next instruction is executed and it stops before the else condition, exiting the loop. If the condition is false, it executes the instructions in the else instruction block and then exits the loop.

if-else Syntax
if (condition) (do_something) else (do_something_else)

The following is an example of how if-else statements can be used:

@echo off 
set /A a=5
set /A b=10
set /A c=%a% + %b%
if %c%==15 (echo "The value of variable c is 15") else (echo "Unknown value")
if %c%==10 (echo "The value of variable c is 10") else (echo "Unknown value")
"The value of variable c is 15"
"Unknown value"
note

Each if-else instruction is placed between () parentheses. If the parentheses are not placed to separate the code of the if-else , the instructions will not be valid as if-else instructions.

Nested if Statement

Sometimes, there is a requirement to have multiple if statement embedded inside each other, for example:

Nested if Syntax
if(condition1) if (condition2) do_something

In this example, only if condition1 and condition2 are met, will the code in the do_something block be executed.

The following is an example of how nested if statements can be used:

@echo off
SET /A a = 5
SET /A b = 10
if %a%==5 if %b%==10 echo "The value of the variables is correct"
"The value of the variables is correct"

Checking Variables

The common use of conditional branching in Batch Script is the checking of variables set in Batch Script itself. The evaluation can be done for both strings and numbers.

Checking Integer Variables

@echo off 
set /A a=5
set /A b=10
set /A c=%a% + %b%
if %c%==15 echo "The value of variable c is 15"
if %c%==10 echo "The value of variable c is 10"
if %c%==15 (echo "The value of variable c is 15") else (echo "Unknown value")
if %c%==10 (echo "The value of variable c is 10") else (echo "Unknown value")
"The value of variable c is 15"
"The value of variable c is 15"
"Unknown value"

Checking String Variables

@echo off 
set str1=String1
set str2=String2
if %str1%==String1 echo "The value of variable String1"
if %str2%==String3 echo "The value of variable c is String3"
if %str1%==String1 (echo "The value of variable String1") else (echo "Unknown value")
if %str2%==String3 (echo "The value of variable c is String3") else (echo "Unknown value")
"The value of variable String1"
"The value of variable String1"
"Unknown value"
note

Note that the evaluation of the condition is "case-sensitive."

Checking Command Line Arguments

@echo off 
echo %1
echo %2
echo %3
if %1%==1 echo "The value is 1"
if %2%==2 echo "The value is 2"
if %3%==3 echo "The value is 3"
if %1%==1 (echo "The value is 1") else (echo "Unknown value")
if %2%==2 (echo "The value is 2") else (echo "Unknown value")
if %3%==3 (echo "The value is 3") else (echo "Unknown value")

The output of the above snippet is below (arguments are 1 2 3)

1
2
3
"The value is 1"
"The value is 2"
"The value is 3"
"The value is 1"
"The value is 2"
"The value is 3"

Special Cases

if defined

A special case of the if statement is the if defined statement, which is used to verify the existence of a variable.

if defined somevariable somecommand

An example of how the if defined statement can be used:

@echo off 
set str1=String1
set str2=String2
if defined str1 echo "Variable str1 is defined"

if defined str3 (echo "Variable str3 is defined") else (echo "Variable str3 is not defined")
"Variable str1 is defined"
"Variable str3 is not defined"

if exists

Another special case of the if instruction is the if exists instruction, used to verify the existence of a file.

if exist somefile.extension do_something

An example of how the if exists statement can be used:

@echo off 
if exist C:\path\myfile.txt echo "File exists"
if exist C:\path\myfile2.txt (echo "File exists") else (echo "File does not exist")
"File exists"
"File does not exist"

if errorlevel

Another special case is if errorlevel, used to check the output codes of the last command executed.

note

Several commands issue integer output codes to indicate the status of the command.

Generally, commands change to 0 if the command was successfully completed and to 1 if the command failed.

The general syntax of this statement is below:

if errorlevel n somecommand

where n is one of the integer exit codes.

goto Statement

The goto command allows you to go to a particular section of code. The target section is labeled with a line at the beginning that has a name with a leading colon.

note

Typically, the execution of a batch file proceeds line by line, and the commands in each line are executed in turn.

For example:

...
goto :label
...some commands
:label
...some other commands

A label can be a line anywhere in the script, including before the goto command.

note

goto commands often occur in if statements.

if (condition) goto :label

A more complex example of how the goto instruction works is shown below:

@echo off 
set /A a=5

if %a%==5 goto :labela
if %a%==10 goto :labelb

:labela
echo "The value of a is 5"

exit /b 0

:labelb
echo "The value of a is 10"
"The value of a is 5"