A batch file or Batch program is an unformatted text file that contains one or more MS-DOS commands and is assigned a .BAT extension. When you type the name of batch program at the command prompt, the commands are carried out as a group.

Applications for Batch Files

Batch files have two major areas of application:

  1. They give you freedom to walk away from the computer while it performs a sequence of operations unattended.
  2. They serve as a place for storing a complex sequence of commands that you want performed without any being missed.

How Batch Files Work

PC-DOS contains a set of routines for recognizing commands. This set is called the command processor. It also contains a set of routines for processing batch files. This group of routine is called the batch file processor.

The command processor looks at the characters that are typed in response to the system-level prompt (usually A> or B>). It sees these characters as soon as you press the enter key. The command processor first tries to match the characters with the resident commands. If there is no luck there, it then looks for a file on disk whose name matches what has been typed.

If you do not include a filename extension (a period followed by one to three characters), the command processor makes a first guess that the extension is .COM and looks for that. Its second guess is .EXE, and its third and last guess is .BAT, which is where we come in. If the command processor finds the .BAT file, it passes control to the batch file processor.

The batch file processor has enough logic in it to read the first line (a command) from a batch file, invoke the command, and when that terminates, read the next command from batch file. This continues until the batch file is exhausted or the user aborts the sequence.

Note: If the command you type in response to the system prompt contains a filename extension, the command processor will look only for files with that specific extension; the batch file processor won’t be invoked unless that extension is .BAT.

Commands used in BATCH files are:

call        choice      echo        for        pause         shift         goto       if       rem

A sample Batch file: Use a text editor to create a batch file:

 Example: 1

EDIT MENU.BAT
@echo off
:Begin
cls
echo.
echo A WordPerfect 5.1
echo B Lotus 1-2-3
echo C Exit
echo.
Choice /:abc Choose an option?
If errorlevel 3 goto Wp
If errorlevel 2 goto Lotus
If errorlevel 1 goto Exit

:Exit
Goto End

:Lotus
cd\lotus
123
cd\
goto Begin

:Wp
cd\wp51
wp
cd\
goto Begin

:end

Example:2

EDIT MYCOPY.BAT
@echo off
Echo MYCOPY.BAT copies any no. of files
Echo to a directory
Echo The command uses the following syntax
Echo mycopy dir file1 file2…
Set todir=%1
:getfile
Shift
If “%1″= = “” goto End
copy %1 %todir%
goto getfile
:End

Set todir=
Echo All done

Example:3

EDIT FILECOPY.BAT
Rem A Batch File FILECOPY.BAT
Echo OFF
Echo# This batch file would perform the following:
Echo#
Echo#
Echo# Step 1-It shows the functions to be undertaken.
Echo#
Echo# Step2-It searches for the file TEXT.TXT
Echo#
Echo#Step3- If the file is not found, an error message
Echo# is displayed and the batch file execution terminates
Echo#
Echo# Step4-If the file is found, the contents of the file is
Echo# displayed and the file is copied with another name specified in the command
Echo# line and the original is deleted
Echo#
Echo# Step5-If the target filename is not specified as a command line parameter,it shows an error message and the execution of the batch file terminates.
Echo#
Echo#
Echo#
Rem Here the actual command functioning starts

PAUSE
CLS
REM PATH=A:\DOS;A:\
IF NOT EXIST TEXT.TXT GOTO NOT_FOUND
:SHOW
TYPE TEXT.TXT
Echo.
IF %1x= =x GOTO NOT_COPIED
COPY TEXT.TXT%1
GOTO END
:NOT_FOUND
Echo #TEXT.TXT file not found!!
GOTO END
:NOT_COPIED
Echo# TEXT.TXT not copied ,’cause don’t know where to copy!!
:END

Related Posts:

You Should Also Check Out This Post:

More Active Posts: