Saturday, 19 January 2013

BATCH FILES

A batch file is nothing more than a collection of DOS commands placed into an ASCII text
file. When DOS executes a batch file, each line in the file is treated as a DOS command and is
executed as if you had typed the command at the system prompt. Their main use is to automate
DOS command sequences you repeat often or are hard to remember.
Each line in the file is treated as a DOS command and is executed as if you had typed the
command at the system prompt.
In addition to standard DOS commands, batch files also have their own subset of commands
which allow you to actually write a batch file as a small program. Branching and iteration are
allowed in these programs.
Also, batch commands may have external parameters passed to them at the time you execute
the file.
A batch file must have the extension .BAT and you execute the commands in it like any
other DOS command: by typing the file's root name, without the extension, at the system prompt.
Parameters are extra pieces of information that you type after many of the DOS commands. For
example, "DIR B: /W" contains the parameters B: and /W. These modify the basic operation of the
command, but are not required by the command.
You pass parameters to a batch file in the same manner; by typing the information after the
batch command, but before tapping the Enter key. 


The parameters may be used in any place in the batch file  where a parameter would
normally be used as part of the DOS command being run.
Markers are used within the batch file to signify which parameter goes where. Markers are
comprised of a percent sign (%) and a single digit between 0 and 9 (that's ten markers in use at any
one time; remember, zero is a number).
As an example:

Assume that a batch file named REPEAT.BAT is on the current drive. This file contains two
commands: "ECHO OFF" which stops DOS from showing commands and  "ECHO %1 %3" where
the ECHO command can show messages on the screen. If, at the DOS prompt you typed "REPEAT
Red Blue Green" your screen would show "Red Green" as shown in the screen capture: 




In this simple example, three parameters were passed to  the batch file and were placed into
the ECHO command in the order received. Only the first and third are shown as only those were
referenced in the batch file.
The parameters and markers were related as follows...
•  %1 is marker 1 and, in this example, represents "Red", the first parameter;
•  %3 is marker 3 and, in this example, represents "Green", the third parameter;
•  %2 would be marker 2 but, in this example, is not present so "Blue", the second parameter,
is ignored.
Note: Marker zero is assigned the name of the batch file in the form you typed it in (i.e., all caps, all
lower case, or a mixture).
In addition to the normal DOS commands, batch files have their own sub command structure.
Following are the sub commands in the order we will discuss them:
•  ECHO:Turns command display on/off or may display a message.
•  REM:Displays a message on the screen.
•  PAUSE:Temporarily stops the batch file execution.
•  GOTO:Jumps to a labeled set of commands in the batch file.
•  IF:Permits conditional operation of any command.
•  SHIFT:Reassigns the relationship of parameters to markers.
•  FOR..IN..DO:Allows iteration subject to defined conditions.
•  CALL:Runs another batch file then returns to first.
•  @:Turns display off for single command

No comments:

Post a Comment