Batch megic
DOS batch processing is notoriaus/y slow, bot ;t does have ;ts uses.
John DeHaven pro vides a compendium of tricks) techniques and

. . .

curlos~ties for your reference.

What is the world's slowest inter-
pretative language? This dubious
honour surely belongs to DOS batch
processing, which runs like grenny.
Let's go ahead and stretch a point
and call DOS batch processing a lan-
guage. It is useful to think of the
statements of this language as in-
cluding the traditional batch proces-
sing commands, ali DOS commands
and the names of any executable
files (including other batch files) that
are available in the default directory
or via PATH.

	Unfortunately, even if we gener-
ously expand ehe definition in this
way, DOS batch processing still isn't
a complete language, since it is not
interactive and cannot add or even
count. Even so,if you hold your jaw a
certain way there is plenty of unsus-
pected power to be found. We'll ex-
piore generation and passing of vari-
ables, file creation, true calls with re-
turn, giant loops under control of
'FOR' and recursion. Batch proces-
sing is not really as boring as IBM
and Microsoft try to make it seem.

	The traditional (IBMIMicrosoft) dis-
cussion lists only six betch proces-
sing commands, and we'll assume
you have a working knowledge of
these: ECHO, FOR, GOTO, IF, PAUSE,
REM and SHIFT. {)f these, ECHO is
useful outside of batch processing,
and FOR can be used outside batch
processing to do some non-trivial
things. Four other commands, COM-
MAND, CLS, EXIT and SETÄare not
usually mentioned as part of batch
processing, although they wouid
rarely be used anywhere else. We
will use them ali here.

Speed

One reason batch processing runs so
slowly is that it makes a disk call for
each line of code. Evidentiy then, a
given betch file will run faster if you
can reduce the number of lines in it.
After you have a batch file running,
you may be able to reduce the num-
ber of lines by combining some of
them in a FOR structure. The follow-
ing rules apply:

1 If statements have no argument,
they may be combined;

2 If statements have the same arqu-

'

ment, they may be combined; and

3 Statements with the same arqu-
ment may also be combined with
statements with no argument.

	Here are some examples to illus-
trate the above points:

FOR %%A IN {CLS VER VOL BREAK
VERIFY SET) DO %%A

FOR %%A IN (CLS A ECHO PAUSE
CLS) DO %%A Insert back-up

diskette

FOR %%@ IN ICLS C: IF A:) DO
%%@ EXIST PP. BAT PP

FOR %%@ IN (MD CD) DO %%~?
\ SDIR1

	These work because certain com-
mands like CLS and PAUSE (and cer-
tain other executable files you might
have created) do not take any arqu-
ments, so when FOR expands them
with an argument, the argument is
ig nored.

	Batch files will run much faster if
you allocate extra 'buffers' with
CONFIG-SYS. Briefly, this specifica-
tion allocates memory for disk l/O,
one buffer per cluster read from dis-
kette. When a disk call is made, these
buffers are checked first, and if the
record is aiready here, no physical
disk reference will be made. The
overhead cost is about 1k per buffer
(not 512k as IBM states), above the
defeult two buffers. Here is how you
can be sure you have those extra
buffers activated:

1 The disk from which you boot
must contain a file called:

'CON FIG.SYS.'

2 This file must contain the state-
ment:

BUFFERS=9
although a larger number will be
OK.

3 For good measure, include the fol-
lowing two lines:

Fl LES = 99
DEVICE=ANSI.SYS

	The first of these will allow a much
larger number of file handles to be
opened than the default eight, at a
cost of only 3783 bytes of memory.
Many programs need more handles
than eight, and you'll be dis-
appointed in the performance of
some of the following examples if
you donrt allow this extra latitude. As
for the assignment of the ANSI.SYS

driver, I can't imagine anyone with
more than 16k of RAM not wa~ting
thisr because it allows control over
screen colours and attribotes, arbit-
rary cursor positioning and even
keyboard reassignment (16-character
keyboard macros at the DOS level
without a fancy prog ram ! )

	lt you are echoing many lines to
the display' you wili find that it is
much faster to put in a single line to
TYPE a file that contains your multi-
line display. This will cost some disk
space, since you will have to create
this extra file. I usually denote such
files as .SCR (for'screen') files.

	If you use labels in a batch file,
those that are most likely to be called
should be put near the beginning, as
the batch processor scans the entire
file from the beginning every time it
looks for a label.

Control

Whether you use ANSI.SYS or not,
the display understands the sequ-
ence ESC-1-2-J to mean 'ciear the
screen'. The CLS command in fact
sends just this sequence to the
screen. This means that you can in-
clude this string in any statements
you ECHO to ttie display and in any
file that you might TYPF to the dis-
play. If you write your batch files
with an editor that allows the inser-
tion of the ESC code, you will be
abJe to echo ANSI control sequences
from the batch file. Some very nice
effects are available. If we let the ex-
pression ' [' stand for the ESC code,
the sequence

CLS

ECHO^[[2Jlnstall the back-up
diskette in drive A. G [[5rn
PAUSE

ECHO 1~2K [[0;1 m
will ciear the screen and display the
prompt message followed by a beep
and a bl~nking pause message. When
a key is struck, the blinking pause
message is wiped out.

	You couid output blank lines with
ECHO followed by two or more
spaces with DOS 2.x, but this no lon-
ger works with DOS 3.x. The sequ-
ence ESC-space-c255tl> will work
with DOS 3.x, the idea being to echo
the invisible character 255 hex. On

