Batch file programming is nothing but the Windows version of Unix shell Programming. Let us 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 that are built in are called internal commands) DOS has something called external commands too such as Format, Undelete, Backup, Fdisk 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 program that executes the command for it and returnes the result (external commands.)
So, why do I need batch file Programs? Say, you need to execute set of commands over and over again to perform a routine asks like backing up important files, deleting temporary files (*.tem,.bak,~.*etc), then it is very difficult to type the same commands over and over aging, To perform such set of commands over and over again , batch files are used. Batch files are DOS what macros are to Microsoft Office and are used to perform an automated predefined set of task over and over again.
So, how do I create batch files? To start enjoying using batch files, you need to learn to create them. Batch files are basically plain text files containing Dos Commands. So the best editor to write your commands would be Notepad or the Dos editor (EDIT). All you need to remember is that a batch files should have the extension. BAT (dot bat) . Executing a batch file is quite simple too , For example, if you create 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, the 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.
While creating a batch file, one thing that you need to keep in mind is that the filename of the batch file should not use the same name as DOS command. For example, if you create a batch file by the name dir.bat and then try to execute it at the prompt, nothing will happen.
Note:This
is because when command.com comes across a command, it first checks to see.
if it is an internal command. If it is not, thern command.com checks if it is a .COM,.EXEor . Bat file, with a matching filename.
All external DOS commands use either a .COM or a .EXE extension, DOS never bother to check if the batch program exist.