Batch Programming Basics PDF
Batch Programming Basics PDF
Author : Nabarun
Categories : Coding
Date : 27-06-2016
IO
E K.
GE
DE
CO
Ever ran a batch file(with .bat extension) while using Windows? I know most of you have. When it comes to download a pirated game or
when it comes to run a command line Windows program, you have used it but never got to know what is this thing or why this kind of program
is there.
1 / 10
To know about this Batch Programming you need to know what is a batch file first. A batch file is actually a kind of script file in DOS, OS/2 and
Windows. This language is used, command line codes and is executed on a command line interpreter. The extension used for such file is .bat,
but in some OS .cmd extension is also included. So today we will teach you the basics of batch programming. We will use notepad++ for
writing batch codes, but you can use any kind of text editor. As always like in every programming language we will start with the "hello world!"
program, which is like an universal base for learning any programming language. Here we go ?
IO
As you can see it's a 3 line program, but all the lines are not used for printing "Hello World!". They have their own functions to make the
K.
program run better. The @echo off statement is used for hiding the cmd echoing of initial paths.
And for print anything the "echo" function is used. Anything right to it will be printed.
E
"pause;" is used for preventing the program from closing without a break.
GE
DE
CO
2 / 10
IO
E K.
GE
DE
Use the above code in your text editor and the file with any name and change the extension from".txt" to ".bat". Now open the save file and
CO
@echo off title Hello World! color E3 echo Hello World! pause;
3 / 10
Remember that the syntax of color attribute is like this:
color [background color][foreground color]
So, you can use any color code instead of "E3", where "E" is the background color and "3" is foreground.
The title attribute is used for the title of your batch program.So, you can use any title instead of "Hello World!".
IO
E K.
GE
DE
CO
A variable is a piece of code which is stored in your RAM, and a variable stores data which can be used in a program whenever it is needed.
Defining a Variable:
IO
E K.
GE
DE
CO
5 / 10
Here's is an example code:
@echo off set variable1=4 echo Variable1 is initialised with %variable1% set /p variable2=Enter the valu
e: echo Variable2 is input as %variable2% pause;
IO
E K.
GE
DE
CO
medianet_width = "800"; medianet_height = "200"; medianet_crid = "532216912"; medianet_versionId = "111299"; (function() { var isSSL =
'https:' == document.location.protocol; var mnSrc = (isSSL ? 'https:' : 'http:') + '//contextual.media.net/nmedianet.js?cid=8CUW5LI24' + (isSSL
? '&https=1' : ''); document.write(''); })();
6 / 10
When you are assigning a value with operators in it, then you should use "set /a [variable name] = [other variable] operator (like +,-,*,/)[another
variable]". For example:
@echo off set var1=4 set var2=5 set /a var3=var1+var2 echo Sum of %var1% and %var2% is %var3% pause;
IO
E K.
GE
DE
CO
@echo off echo Calculator :Cal set /p a=Enter 1st Value: set /p b=Enter 2nd Value: set /a sum=a+b set /a
difference=a-b set /a product=a*b set /a quotient=a/b echo Sum of %a% and %b% is %sum% echo Difference
between %a% and %b% is %difference% echo Product of %a% and %b% is %product% echo Qoutient of %a% divid
ed by %b% is %quotient%(Integer value) pause;
7 / 10
If Statement in Batch Programming:
If statement is used to check if an expression is true or false. If the expression is true, then the codes based on it will be executed. The syntax
is:
if (expression) statement
IO
@echo off set /p var1=Please rate us out of 3: if %var1%==0 echo Null score if %var1%==1 echo Ok Score i
f %var1%==2 echo Good Score if %var1%==3 echo Best Score pause;
E K.
GE
Output is like this:
DE
CO
8 / 10
Blocks and Goto statement in Batch Programming:
Here Blocks are a bunch of statements and declarations. Using block we can treat a group of statement as a single statement and can worked
upon.
To define a block we use the columns(':'). Everything on the right of ':' will be defined as a block. For example
IO
Now let's Acknowledge you about "goto" statement. Goto statement makes a statement to jump to block of statement in the program.
K.
An example code with usage of these functions is given below:
E
@echo off set /p var1=Select Block number from 1 and 2 : if %var1%==1 (goto Block1) if %var1%==2 (goto B
GE
lock2) :Block1 echo This is Block1 pause; exit; :Block2 echo This is Block2 pause;
DE
CO
9 / 10
IO
E K.
The topic is over for now. Please practice everything that you have learned. ?
GE
And remember batch files work upon DOS commands, so better learn DOS commands to make efficient use of it.
If you have any doubt, then you are free to comment it below and we will try to reply as quick as possible.
DE
If you liked it, then please Share our page, so that more and more people get to know about it and learn new things?.
CO
10 / 10
Powered by TCPDF (www.tcpdf.org)