Thursday, 18 April 2013


Batch file programming is the native programming offered by the Microsoft Windows Operating System. Batch file is created using any text editors like notepad, WordPad, WinWord or so on, which comprises of a sequence of built-in commands used to perform some often done tasks. Whenever a Batch program is executed, it was interpreted line-by-line by the CLI (Command Line Interpreter) command.com or the cmd.exe. Batch file is really helpful in automating tedious tasks and for maintaining system logs. The commands used while creating a batch file are case insensitive, in the sense that it may accept both small and upper case letters.

How to create a Batch Program:

As said earlier, batch programs can be written using any of the text editors such as notepad, wordpad and so on, but notepad is the most often used text editor in such cases. Like any other programing languages, lets start our first program with the ‘Hello World’ program.

1. Open up a notepad and type the following.

@echo off
Echo Hello World
pause

2. Save the file with any name you wish, but make sure that you save the file extension with .bat, in this case I am saving this file as ‘first.bat’.

3. Just double click to execute the batch file that you have created now. And the output looks like,

Hello World
Press any key to continue...

Let me explain what does the above given program does,‘echo’ is the command used to print text on the screen, so whatever that follows the echo
command will be displayed on the output screen. This command is just like the ‘printf’ statement in the C language. When you type the echo command alone, then it will tell you whether the ‘echo is ON’ or ‘echo is OFF’.
It’s always recommended to turn the echo off, else it will display the prompts like (C:\>) and so on. In
order to avoid the prompts being displayed, the echo is turned off by using the command “@echo off” or
simply by using the “echo off”.
“Echo Hello World” will display the “Hello World” on the output screen, and the pause command is used
to wait for the user interaction, whether to proceed further or not. If the pause is not used, then the batch
will terminate immediately after displaying the “Hello World”.

Internal and External Commands

There are two types of commands that we can run from a command prompt, and they were,

1. Internal commands
2. External commands.

Internal Commands
Internal commands are nothing but the built-in commands that are shipped along with the
operating system, for example, echo, cls, del, dir were few of the well known internal commands.

External Commands
External commands are the commands that are often created while installing a new application and these commands mostly have no use except calling that application and support files. Few external commands can only be executed in the ‘Run’ dialog box (start ? Run), but not on the command prompt, and those commands include ‘firefox’. The ‘firefox’ command can be executed only from the run line, that too if the firefox application is installed on that machine and it won’t work on the command prompt. Likewise the ‘firefox’ there are various other external commands such as the “PsTools” which includes commands like, PsExec, PsFile, PsGetSid, PsInfo, PsKill, PsList, PsLoggedOn and so on.

Batch Operators

Similar to other programming languages, batch program do support various operators for performing operations like arithmetic and logical operations, bitwise AND, OR, NOT, shifting and redirection operation and separators and grouping operators.

OperatorsDescription
( )Grouping
! ~ -Unary operators
* / % + -Arithmetic operators
<< >> < >Logical shift and re directional operators
&Bitwise and
^Bitwise exclusive or
|Bitwise or
= *= /= %= += -= &= ^= |= <<= >>=Assignment operators
,separator
&&For using Multiple commands
||For executing one from many commands

The above given were the operators available in Batch file programming for performing arithmetic and logical operations.

Conditional Statements

The conditional statement enriches the features of the batch file programming. The conditional statements are widely used for making a decision, and in accordance to the decision taken, the result or the output is produced.
The primary decision making statements used in batch programs are,
IF and IF NOT
The decision making statements deals with error levels, string matching and files.

The following piece of code will help you in a better understanding of how it deals with files and folders,

@echo off
if exist C:\windows.
(
echo Found
)
else
(
echo Not found
)
Pause

In this case, the program will check for the directory C:\windows, and if it exists then it will display the message ‘Found’ else it will display ‘Not Found’. The following is the output generated by the program given above,

Found
Press any key to continue...