Category : Batch File Utilities - mostly for DOS
Archive   : BATDOC.ZIP
Filename : NOVBH_5.DOC

Output of file : NOVBH_5.DOC contained in archive : BATDOC.ZIP



from the BATHINTS library... part of the BATPOWER CONFERENCE from:


Baltimore, Maryland 1-301-488-7461


An Introduction to Batch File Programming

Each time you start your computer, whether with the big red switch or by
pressing CRTL-ALT-DEL keys simultaneously, DOS searches for a file called
AUTOEXEC.BAT, where the user can place commands to be executed each time
the computer is started. This is a very useful feature, since this option
allows the user to tailor the way that his or her system presents itself
each and every time the computer is started. But before going too far, it is
time that something be said of batch files in general, since this is after all,
the fifth NOVICE BATHINT and I have as yet to talk about batch files!

Batch files are nothing more than a series of commands placed on separate lines
in a file that has the extension .BAT. Apart from a few special commands that
are used only (or primarily) in batch files, the commands in a batch file are
the very same commands that the user normally enters at the command line...
and with few exceptions, any executable command can be placed in a batch file.
The utility of the batch file is in the execution of commands that are used
often... since these commands can be stored in a batch file, rather than being
entered manually each and every time the same series of commands needs to be
executed. For example, if you always wanted to use a prompt with the time and
directory path displayed on the command line and wanted your command search
path to look in the subdirectories named DOS and BAT in drive C, you could
enter the appropriate commands each time the computer is started, or you could
enter the commands once (and only once) in a batch file and then just call the
name of the batch file from the command line. Obviously it would be more
efficient to use a batch file for such commands. A more common use of the
batch file is in the execution of a software application (i.e. WordPerfect,
Lotus 123, Procomm, Sidekick, etc.). Before looking at specific batch file
commands, consider the following example batch file (S.BAT) that loads the
program Sidekick from a hard disk:



In this example, it is assumed that S.BAT resides in the current drive and
directory or can be found in one of the drive/subdirectories that were named
in the PATH variable. If the user enters the letter "s" at the command line,
S.BAT is executed line by line from the top to the bottom. Each of the four
lines in the batch file is an executable statement... first the system changes
drives to drive C (the hard disk), then changes directories to Sidekick, then
calls the program SK and finally changes back to the root directory (of drive
C, the current drive). The user could have entered each of these lines at the
command line, pressing the ENTER key after each line... acheiving the same
result as entering S. Obviously, it is much more efficient to save these
commands in a file... a BATCH file... and call the batch file when needed, than
it is to manually enter the four command lines each time the user wants to load

The example shown above is intented only to show that a batch file is nothing
more than a series of commands placed into a file and called into orderly
execution by entering the name of the (batch) file. These special files must
have the extension .BAT placed after their filenames in order for DOS to
recognize them as BATCH files.

Batch files have several very powerful features, not all of which include:

1. If a batch file is created that is called AUTOEXEC.BAT and if this
batch file is placed in the root directory of of the disk that is
used to start the computer, that batch file will be executed
immediately after DOS carries out the commands placed in its
configuration file (CONFIG.SYS)... though CONFIG.SYS need not exist
for this to be true... and this batch file will be executed at the
time the computer is started EVEN THOUGH THE USER DID NOT ENTER THE
search out and execute the commands placed in AUTOEXEC.BAT, if that
file exists, can be over-ridden by a special command placed in the
CONFIG.SYS file, so that AUTOEXEC.BAT is not executed automatically

2. Conditional statements can be placed into batch files such that
certain commands or groups of commands will be executed (or not
executed) only if certain conditions are true or false. The user
can "test" the condition of his/her system and selectively execute
commands automatically.

3. Batch files can be paused during execution for defined periods of
time, can be made to wait for information from the user (entered at
the keyboard) and can be paused until a particular time of day.

The many different commands that can be placed in batch files are the subject
of individual BATHINTS, but before proceeding any further, let's revisit the

With the knowledge of the PATH and PROMPT commands (from NOVBH_3 and _4), a
simple AUTOEXEC.BAT file can now be described:


path c:\bat;c:\dos;c:\util
prompt $p$g

Simple eh? This example sets the search path to three subdirectories on drive
C (bat, dos and util), sets the value of the system prompt to display the
current drive and subdirectory (with a little right carrot ">") and then clears
the screen. When a computer is started with an AUTOEXEC.BAT file, the user is
no longer prompted for the time and date as he/she would be if no AUTOEXEC.BAT
file had been found by DOS. So if you want to enter the time and date for
your system you would change the example above to:

path c:\bat;c:\dos;c:\util
prompt $p$g

Naturally, you could place these commands in any order. Many users have clocks
built into their system or onto multifunction boards... and many of these
require that a program like "getclock" or some similarly named program, be run
to set the system clock. In such a case, the AUTOEXEC.BAT might read:

path c:\bat;c:\dos;c:\util
prompt $p$g

In this case, the program "getclock" would have to reside in the root directory
of the start-up disk, or in one of the drive/subdirectory combinations named
in the PATH command.

Any manner of commands can be placed in the AUTOEXEC.BAT file to configure your
hardware and software to your exact needs. For example, the user might want to
load sidekick each and every time the system is started, in which case the file
might be changed to read:

prompt $p$g

To fancy things up, the user might also want to hide the individual commands so
that each line in the batch file is not "echoed" to the screen. In this case,
the file could be changed to read:

echo off
path c:\bat;c:\dos;c:\util
prompt $p$g

The "echo off" statement (the ECHO command is the subject of the next NOVICE
BATHINT... NOVBH_6.DOC) is used to suppress the display of the individual
commands on the screen so that the user isn't bothered or confused by the
commands being automatically and sequentially executed.

The AUTOEXEC.BAT file has many uses, some of which are listed below. If you
are still having trouble with batch files do not despair... your skills will
grow with time and practice... provided that you continue to read BATPOWER !


1. installation of memory resident programs

2. placing programs in virtual (RAM) disks

3. to set search paths

4. to set system prompt

5. to place COMMAND.COM in a virtual (RAM) disk so that it is
always available to DOS

6. to initialize ports and devices for standard use

Well this should get you started on the road to easier computing (I hope!).
If you have had any trouble understanding the contents of this or any other
BATHINT, consult your DOS manual or drop a question in the BATPOWER message
area of your favorite BBS... or go on to the next BATHINT... and perhaps things
will become more clear as you gather together the essential kernal of knowledge
that will eventually instill in you the uncontrollable urge to shout AH HA !

********************************************************** David Creasey