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

Batch Programming Basics PDF

This document provides an introduction to batch programming basics, including: 1) It explains what a batch file is and how the "Hello World" program works in 3 lines of code. 2) It covers basic batch programming topics like setting color and title, using variables, taking user input, and using basic operators. 3) It demonstrates if statements, blocks, and goto statements to control program flow. The document is intended to teach programming beginners the essential foundations of writing and running batch files on Windows.

Uploaded by

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

Batch Programming Basics PDF

This document provides an introduction to batch programming basics, including: 1) It explains what a batch file is and how the "Hello World" program works in 3 lines of code. 2) It covers basic batch programming topics like setting color and title, using variables, taking user input, and using basic operators. 3) It demonstrates if statements, blocks, and goto statements to control program flow. The document is intended to teach programming beginners the essential foundations of writing and running batch files on Windows.

Uploaded by

Antun Matkovic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Batch Programming Basics

Author : Nabarun

Categories : Coding

Date : 27-06-2016

Batch Programming Basics

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 ?

The "Hello World!" program:

@echo off echo Heyo World! pause;

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

view your 1st batch program.

Now we will learn the basics of Batch programming Topic wise:

Setting color and title for your Batch program:

To know the color codes, visit this page.


Now look at the below

@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!".

The output of the above code will look like this:

IO
E K.
GE
DE
CO

Variables in Batch Programming:

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:

A variable in a batch file can be defined as set [Variable name]=[Variable value]


You can initialize a value with variable or you can input it though the program. We will show you how to use user input in the next topic below.
4 / 10
Here is a code with variable usage.

@echo off set variable=4 echo Variable used is %variable% pause;

(Note:In %variable% , %_% should be used to print a variable.)

The output will be like this:

IO
E K.
GE
DE
CO

User input in Batch Programming:

We can make the program more interacting by adding user input.


To use user input to define a variable you should use this code-
set /p [Variable name]=[Variable value]

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;

Output is like this:

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(''); })();

Using basic operators in Batch Programming:

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;

Output is like this:

IO
E K.
GE
DE
CO

Similarly we can make a simple calculator using the code below:

@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

An example code is-

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

:Block1 echo 1st line of Block1 echo 2nd line of Block2

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

The output will be like this:

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)

You might also like