Batch-File Training
Batch-File Training
Outline
• Introduction on Batch File
• Batch file - Commands
• Batch file - Variables
• Batch file - Operators
• Batch file - Conditional and Looping
• Batch file - Exit/Error codes
1.
Batch file -Introduction
What is a batch file?
▷ A batch file or batch job is a collection, or list, of commands that are
processed in sequence often without requiring user input or intervention.
▷ Batch file commands are case-insensitive.
▷ With a computer running a Microsoft operating system such as Windows,
a batch file is stored as a file with a .bat file extension.
▷ Other operating systems may define a batch job in a shell script,
containing a list of commands to be executed one after the other.
▷ Batch files are often used to help load programs, run multiple processes at
a time, and perform common or repetitive tasks.
▷ Extension of a batch file will be .bat or .cmd
Do’s
▷ Documenting code with comments
▷ Validating input
▷ Check variables before using them
▷ Indentation
Don’t s
▷ Avoid one-liner codes (multiple command lines joined into
a single one by ANDs and ORs) and use a block of code.
▷ Avoid nested block codes (nested if else) and use
subroutines instead.
▷ Don’t use variable names as command names
2.
Batch file -Commands
Batch file- Commands
▷ ECHO ▷ MD
▷ SET ▷ MOVE
▷ CD ▷ PATH
▷ ATTRIB ▷ RD
▷ CHOICE ▷ REM
▷ CLS ▷ TITLE
▷ CMD ▷ PAUSE
▷ DEL
▷ DIR
▷ EXIT
▷ HELP
ECHO command
▷ The batch command ECHO is used for echoing
commands on/off and printing message to the
console.
▷ Example
@echo OFF
echo HelloWorld!
▷ Output:
HelloWorld!
▷ echo is also used for deciding whether or not to
display the command itself.
SET command
▷ The batch command SET displays the list of environment
variables of the system.
▷ Example:
SET var=Apple
TITLE command
▷ The batch command TITLE sets new title for output
console.
▷ Example:
TITLE HelloWorldProject
PAUSE command
▷ The batch command PAUSE is used for holding the output screen till
user enters a variable or a value.
Example:
ECHO hi
pause
CD command
▷ The batch command CD helps in navigating through different
directories and changing directories or displaying current directory.
Example:
:: CD without any parameters displays the current working directory
CD
:: Changing to the parent directory one level up
CD..
:: Changing the path to Programs
CD\Programs
REM command
▷ The batch command REM signifies comments in the batch script.
▷ Instead of REM we can also use ::
Example:
REM This is a comment
:: This is also a comment
Anything written after REM is interpreted as a comment and is not executed in
batch programs.
CLS command
▷ The batch command CLS clears the screen.
▷ Example:
CLS
CMD command
▷ The batch command CMD invokes a new command prompt window.
▷ Example:
CMD
PATH command
▷ The batch command PATH displays the path variable or it can be
used to set path variable.
▷ Example:
ECHO %PATH%
HELP command
▷ The batch command HELP command we can know about all the
other commands used in batch file or command prompt.
▷ Example:
HELP
MKDIR command
▷ The batch command MKDIR or MD command is used to create a new
directory/folder.
MKDIR <Folder Name>
▷ Example:
MKDIR helloWorld
MD helloWorld
CHOICE command
▷ The batch command CHOICE provides a list of options to the user.
Example:
ECHO You want coffee?
ECHO Enter Y for yes
ECHO Enter N for no
CHOICE /c YN /m "Yes or No"
Output:
You want coffee?
Enter Y for yes
Enter N for no
Yes or No [Y,N]?
ATTRIB command
▷ The batch command ATTRIB is used to display the file attributes or
set an attribute to a file in the working directory.
Example:
:: To display attribute of note.txt
ATTRIB note.txt
:: To make it read only by adding 'r'
ATTRIB +r note.txt
:: To make it hidden by adding 'ah'
ATTRIB +ah note.txt
:: To remove attribute read only
ATTRIB -r note.txt
3.
Batch file -Variables
Batch file- Variables
▷ Global Variables ▷ Local Variables
By default all the variables created ▷ Syntax:
are global variables. SETLOCAL
<Local variable>
▷ Syntax: ENDLOCAL
SET variable_name=variable_value ▷ Example:
▷ Example: SETLOCAL
SET var=HelloWorld SET var=var is local
echo %a% ->To print the value ECHO %var%
▷ For numeric values we have to use ENDLOCAL
/A.
▷ Example:
SET /A var=20
4.
Batch file -Operators
Batch file- Operators
▷ Unary Operators
▷ Arithmetic Operators
▷ Relational Operators
▷ Logical shift & redirectional
Operators
Unary Operators
▷ ~(Tilde) Operator -> Used for shortening the long directory names
▷ ! (Boolean Operator)
▷ -(Unary Minus)
Arithmetic Operators
▷ Based on the priority the operators are listed below
▷ () -> Grouping Operator (Highest Priority)
▷ / -> Division Operator
▷ * -> Multiplication Operator
▷ % -> Modulus Operator
▷ + -> Addition Operator
▷ - -> Minus Operator
Relational Operators
▷ EQU -> Equal to
▷ NEQ -> Not Equal to
▷ LSS -> Less than
▷ LEQ -> Less than or Equal to
▷ GTR -> Greater than
▷ GEQ -> Greater than or Equal to
5.
Batch file -Conditional and Looping
statements
Batch file- Conditional Statements
IF condition IF-ELSE condition
Syntax: Syntax:
1 0 Program successfully
executed.
2 1 Incorrect function.
3 2 system cannot find the file in
that specified location.
4 3 system cannot find the
specified path.
5 5 Access is denied. So the user
has no access right to
specified resource.
And more
7.
Batch file -References
Batch file-References
▷ Some of the useful sites are given below:
http://www.trytoprogram.com/batch-file/
https://www.tutorialspoint.com/batch_script/index.htm