Dos Programming
Dos Programming
BATCH FILES
Batch File
• Batch Script is incorporated to automate
command sequences which are repetitive in
nature.
• Scripting is a way by which one can perform
this necessity by automating these command
sequences.
Basic Features
• Some of the features of Batch Script are:
• It Can read inputs from users so that it can be
processed further.
• It Has control structures such as for, if, while,
switch for better automating and scripting.
• It supports advanced features such as
Functions and Arrays.
• It Supports regular expressions.
Basic Features
• Batch scripts are stored in simple text files
containing lines with commands that get
executed in sequence, one after the other.
• These files have the special extension BAT or
CMD.
• Files of this type are recognized and executed
through an interface (sometimes called a shell)
provided by a system file called the command
interpreter.
• On Windows systems, this interpreter is known as
cmd.exe.
Basic Features
• Typically, to create a batch file, notepad is
used.
• This is the simplest tool for creation of batch
files.
• Next is the execution environment for the
batch scripts.
• On Windows systems, this is done via the
command prompt or cmd.exe.
• All batch files are run in this environment.
Creating Batch Files
• After your batch file is created, the next step is to save your
batch file. Batch files have the extension of either .bat or
.cmd. Some general rules to keep in mind when naming
batch files
• Try to avoid spaces when naming batch files, it sometime
creates issues when they are called from other scripts.
• Don’t name them after common batch files which are
available in the system or reserve words.
• Remember to put the .bat or .cmd at the end of the file
name.
• Choose the “Save as type” option as “All Files”.
• Put the entire file name in quotes “”.
Executing Batch Files
If Statement
The first decision-making statement is the ‘if’
statement. The general form of this
statement in Batch Script is as follows:
Syntax :
if(condition) do_something
If Statement
Example1:
@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 Statement
Example2:
@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/else Statement
Syntax:
If (condition) (do_something) ELSE (do_something_else)
Example:
@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")
If/else Statement
Example2:
@echo off
SET str1=String1
SET str2=String2
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")
If/else Statement
Example3:
@echo off
echo %1
echo %2
echo %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")
Arithmetic operators
Arithmetic operators : Example
@echo off
SET /A a=5
SET /A b=10
SET /A c=%a%+%b%
echo %c%
SET /A c=%a%-%b%
echo %c%
SET /A c=%b%*%a%
echo %c%
SET /A c=%b%/%a%
echo %c%
SET /A c=%b% %% %a%
echo %c%
Relational operator
Relational Operator : Example
@echo off
SET /A a=5
SET /A b=10
if %a% EQU %b% echo A is equal to than B
if %a% NEQ %b% echo A is not equal to than B
if %a% LSS %b% echo A is less than B
if %a% LEQ %b% echo A is less than or equal B
if %a% GTR %b% echo A is greater than B
if %a% GEQ %b% echo A is greater than or equal to B
Logical Operator
Logical operator: Example
• @echo off
• SET /A a=5
• SET /A b=10
• IF %a% LSS 10 (IF %b% GTR 0 (ECHO %a% is
less than 10 AND %b% is greater than
• 0))
Logical operator: Example
@echo off
SET /A a=5
SET /A b=10
IF %a% GEQ 10 (
IF %b% LEQ 0 (
ECHO %a% is NOT less than 10 OR %b% is NOT greater than 0
) ELSE (
ECHO %a% is less than 10 OR %b% is greater than 0
)
) ELSE (
ECHO %a% is less than 10 OR %b% is greater than 0
)
Logical Operator:Example
@echo off
SET /A a=5
IF NOT %a%==6 echo "A is not equal to 6”
Assignment Operator
Assignment operator : Example
@echo off
SET /A a=5
SET /A a +=5
echo %a%
SET /A a -=5
echo %a%
SET /A a *=5
echo %a%
SET /A a /=5
echo %a%
SET /A a %=5
echo %a%