# Home

23 Nov, 2008

## Batch File Programming

Posted In: Tips Tricks

# ehot

## Response

• No comments yet

## Interesting Article

Batch file programming is nothing but the Windows version of Unix Shell
Programming. Let's start by understanding what happens when we give a DOS
command. DOS is basically a file called command.com
It is this file (command.com) which handles all DOS commands that you give at the
DOS prompt---such as COPY, DIR, DEL etc. These commands are built in with the
Command.com file. (Such commands which are built in are called internal
commands.).DOS has something called external commands too such as FORMAT,
UNDELETE, BACKUP etc.

So whenever we give a DOS command either internal or external, command.com
either straightaway executes the command (Internal Commands) or calls an external
separate program which executes the command for it and returns the
result (External Commands.)

So why do I need Batch File Programs? Say you need to execute a set of commands
over and over again to perform a routine task like Backing up Important Files,
Deleting temporary files(*.tmp, .bak , ~.* etc)
then it is very difficult to type the same set of commands over and over
again. To perform a bulk set of same commands over and over again, Batch files
are used. Batch Files are to DOS what Macros are to Microsoft Office and are used
to perform an automated predefined set of tasks over and over again.

So how do I create batch files? To start enjoying using Batch files, you need to
learn to create Batch files. Batch files are basically plain text files
containing DOS commands. So the best editor to write your commands in would be
Notepad or the DOS Editor (EDIT) All you need to remember is that a batch file
should have the extension .BAT(dot bat)Executing a batch file is quite simple
too. For example if you create a Batch file and save it with the filename
batch.bat then all you need to execute the batch file is to type:

C:\windows>batch.bat

So what happens when you give a Batch file to the command.com to execute?
Whenever command.com comes across a batch file program, it goes into batch
mode. In the batch mode, it reads the commands from the batch file line by
line. So basically what happens is, command.com opens the batch file and reads
the first line, then it closes the batch file. It then executes the command and
again reopens the batch file and reads the next line from it. Batch files are
treated as Internal DOS commands.