0% found this document useful (0 votes)
31 views

Dos Programming

Batch files allow users to automate repetitive command line tasks on Windows systems. They store sequences of commands in simple text files with .bat or .cmd extensions. Common features include control structures like IF/ELSE statements, variables, and arithmetic/logical operators to conditionally execute blocks of code. Batch files are created and edited in Notepad then executed from the command prompt where they automate tasks through command sequences.

Uploaded by

pppeoeokeok
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Dos Programming

Batch files allow users to automate repetitive command line tasks on Windows systems. They store sequences of commands in simple text files with .bat or .cmd extensions. Common features include control structures like IF/ELSE statements, variables, and arithmetic/logical operators to conditionally execute blocks of code. Batch files are created and edited in Notepad then executed from the command prompt where they automate tasks through command sequences.

Uploaded by

pppeoeokeok
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

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

• Creating Batch Files : Batch files are normally


created in notepad.
• Hence the simplest way is to open notepad
and enter the commands required for the
script.
Saving 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

• Step 1 − Open the command prompt


(cmd.exe).
• Step 2 − Go to the location where the .bat or
.cmd file is stored.
• Step 3 − Write the name of the file and press
the Enter button to execute the batch file.
Some Batch File Commands
• ECHO :
• Echo is used to display the message / value of variable.
• Echo off
• The purpose of this command is to turn off this display.
• The command "echo off" turns off the display for the whole script,
except for the "echo off" command itself.
• The "at" sign "@" in front makes the command apply to itself as
well.
• Example : @ Echo Off
echo "The program is started“
echo "The program is completed“
Some Batch File Commands
• Rem : To write the comment statement.
• Example :
• Rem “This is not for execution”
Command line argument
Batch Script supports the concept of command
line arguments wherein arguments can be
passed to the batch file when invoked.
The arguments can be called from the batch
files through the variables %1, %2, %3, and so
on.
Command line argument
• The following example shows a batch file
which accepts 3 command line arguments and
• echo’s them to the command line screen.
• @echo off
• echo %1
• echo %2
• echo %3
Some Batch File Commands
Set :
• The other way in which variables can be initialized is
via the ‘set’ command.
• Syntax:
set /A variable-name=value
where,
• variable-name is the name of the variable you want to
set.
• value is the value which needs to be set against the
variable.
• /A – This switch is used if the value needs to be
numeric in nature.
Some Batch File Commands
Set :
• Example:
@echo off
set message=Hello World
echo %message%
To display the value of the variable, the variable needs to be enclosed in the
% sign
Some Batch File Commands
Set :
• Example:
@echo off
SET /A a=5
SET /A b=10
SET /A c=%a% + %b%
echo %c%
Some Batch File Commands
Set:
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%
Some Batch File Commands
Set /p : To take input from the user.
Example :
Set /p x=enter a number
Echo %x%
If Statement

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%

You might also like