Académique Documents
Professionnel Documents
Culture Documents
by Barry Simon
Introduction
One of the tools that most distinguishes the experienced DOS user
from others is the effective use of BATch files. In this article, I
will discuss some of the more advanced features of BATch files and
their commands. I call this tutorial "intermediate" because I have
tried to write in a manner accessible to those with only a little
experience writing BATch files.
o loads my thesaurus;
o after I am finished with the word processor, clears the macros and
runs RELEASE to boot the thesaurus and mouse menu from memory.
BATch files must be pure ASCII text files with separate commands
on distinct lines separated by carriage return/line feed pairs.
To create your BATch files, you can use EDLIN, a text editor, or any
word processor that produces straight ASCII text files.
Parameters
editor %1
foo filename
When the BATch processor comes to the %1, it looks for the first
parameter following "foo" and replaces %1 by that parameter, in
this case by filename.
To be more precise, DOS parses the command line calling the BATch
file into distinct "words" separated by one of four delimiters:
<space> , ; =
foo.cpcugisgreat
foo.bat will be run and %0 will have the full 17 character string
assigned to it. Similarly, since DOS knows that file names are no
more than 8 characters, the BATch file 12345678.bat will be run if
you type in
12345678ABCDEFGHIJ
then typing
foo junk
The real limitation of BATch file parameters is that they are not
variables. You cannot manipulate them by parsing them and cannot
change their values.
Labels
While DOS ignores such lines, the string following the : becomes a
label for the GOTO command which I'll discuss next. The first word or
the first eight characters of the first word become the name of that
label. You can also place comments after a label name if you separate
these comments with a space.
GOTO
The most significant way in which BATch files go beyond the DOS
command line concerns two logical control structures, the GOTO and
the IF commands. The line
goto <label>
will send the BATch file to the line immediately following the
line on which the label appears. If the label appears more than
once, its first appearance in the file is used. If the label does
not appear, the processing of the BATch file is abruptly ended
with the message "label not found".
goto %1
:ff
copy ff.txt prn
goto end
:boldon
copy boldon.txt prn
ETC
One problem with the limited logic allowed by BATch files is the
difficulty of error checking. With the above, if you forget to
put in a parameter or put in an illegal value, you would get the
rather inelegant "label not found" message. Alternatively, you
could replace the "goto %1" with separate lines such as:
if %1 == ff, goto ff
PAUSE
The next BATch command that I'll discuss, PAUSE, unlike GOTO and
parameters makes sense at the DOS command line. For straight DOS
usage, this is pointless but for environments like CED or TallScreen
which allow multiple commands on one line, it can be useful. What
PAUSE does is display the message:
and pause the processing of the BATch file until a key is struck.
Before the message is displayed, the keyboard buffer is flushed to
prevent any stray keystrokes left over from previous operations
from bypassing the effect of the PAUSE command.
There are three rather distinct uses of the PAUSE command. The
most well known is to allow the user to take an action like
inserting a diskette into a drive. Often, you will want to echo a
message to the screen before the PAUSE command such as:
The IF command
The second and last element of flow control provided by DOS is the
IF command. Like pause, it also makes sense at the DOS command
line. The syntax is
As the last line shows, the syntax for checking strings is to use
two equal signs. Strings are sequences of characters not
including the standard delimiters (space, comma, semicolon or
equal) or redirection signs. These redirection signs but not
delimiters can appear if the string or part of the string is
surrounded by single or double quotes. If an illegal string is
used, DOS will reward you with "bad command or filename". There
is a standard problem faced by users first exposed to BATch files.
Often, you will want to test for an empty string, for example to
give an error message if the user failed to provide a necessary
parameter. Empty strings are not allowed so that the line
if %1 == echo whoops
This may seem like a great way of moving a file from one directory
to another so that
will move foo.txt from the current directory to junk. But if the
directory junk doesn't exist, the first line will give the error
message "Invalid directory" and the second line will erase the
file which has NOT been copied. Thus, as a protection one should
add before the "erase %1" line
with an appropriate label and error message. There are many other
places where such error checking can be invaluable.
Unfortunately, "if exist" works with path names only in DOS 3.x
and not in DOS 2.x.
if errorlevel 2
command/c foo
command/c foo
FOR...IN...DO
will issue three separate commands copying the three files to prn.
You can also force commands which don't treat wildcards "properly"
to do so. For example, if you want to change the date/time stamp
on a file foo.txt to the current date and time, the command
copy/b foo.txt+,,
If you want to stop such a command before it has run its course,
Ctrl-C will give you the opportunity to break the loop. In
addition you should be warned that a FOR...IN...DO loop is
like a BATch file: if the expression called after the keyword "do"
involves a BATch file, processing will not return to the
FOR...IN...DO loop. It will only be processed one time even if
you have several possibilities in your list. As happens for BATch
files, placing command/c will prevent the chain breaking so
issued at the DOS command line would run the BATch file foo.bat
successively with each file in the default directory as a
parameter.
SHIFT
If SHIFT were only good for accessing parameters beyond the initial
ten, it would be of limited use. I cannot find any interesting
use of this aspect of the command. Its real use lies in the
possibility of having a real loop within a DOS BATch file. Here
is a sample "touch.bat" expanding the one line procedure for
updating the date/time stamp of a file:
:top
if %1x=x goto end
for %%a in (%1) do copy %%a+,,
shift
goto top
:end
With this BATch file, you can issue the command touch followed by
several parameters, each with wildcards. The basic operation is
applied to each file meeting any of the filespecs listed as
parameters. Of course one could probably do just as well with the
single line
ECHO
echo off
When echo is off, you can display messages by preceding the
desired communication with the keyword ECHO. The default for
BATch file should be echo off and there are patches for
COMMAND.COM to make that the default. I have avoided this patch
because such a patch is not immediately available for new versions
of DOS. Unless you make this patch, most of your BATch files
should begin with
echo off
cls
echo ^L >prn
(where ^L means control-L the form feed character) will not send
only a form feed to your printer. It will send an extra line
feed. Also, you will not be able to send escape codes to your
screen or printer directly from the DOS command line. DOS reacts
to your <Esc> key by aborting the current command instead of
placing an escape character at the cursor. With many editors you
can put escape characters into a file and so write BATch files to
send escape codes to the screen or printer.
echo ^G >prn
echo .
(note the period) produce blank lines but this has not always been
true and has led to some rather fancy programs with installation
files showing bunches of dots on the screen. There is a rather
simple solution. To get a blank line try
where <char 255> means including the character with ASCII code
255. You can't enter this in all editors but in many (e.g.
EDLIN), you can by holding down the <Alt> key and hitting 2 5 5 on
the numeric keypad. Lest you be wary of using an undocumented
feature of DOS, this trick merely relies on the documented feature
of "echo" to send any character even those with codes above 128 to
the screen: <char 255> is a blank character. You can also use one
of these characters to anchor down a message that you don't want
to start in column one.
Even if echo is off, the last line in the BATch file will appear
on the screen if that line does not end in a carriage return /line
feed pair. For this reason, you will probably want to be sure
that the last lines in your BATch files have such a CR/LF pair at
the end. The exception is if the last line is "cls". In this
case the echo to the screen is unimportant and adding a CR/LF will
sometimes place the prompt on line three instead of line two.
Using ANSI.SYS
If you are writing for a "mass market", you cannot assume that the
user of your BATch files has ANSI.SYS installed but if you are
writing for yourself or for a colleague, you can be sure it is
installed. ANSI.SYS provides a simple way of controlling colors
and, to get really attractive screens, the location of messages.
You should consult your DOS manual (or the DOS technical reference
for DOS 3.x) to learn how to move the cursor. For example, if you
want to start with echo off and then only erase the line "echo
off" without clearing the whole screen, try:
echo off
echo <esc>[A<esc>[K
Also, you can end a BATch file by displaying a menu with choices
labelled "a,b,c" and have BATch files called a.bat, b.bat, c.bat
to run depending on the user's response.
Global variables
SET name=string
NAME=string
then the BATch file will go to the label success. If foo has no
current value, %foo% is replaced by the empty string. Several
warnings are in order. First, this procedure is undocumented.
However, it has been constant from DOS 2.0 through 3.2 and is so
close to a documented procedure available to programs that it is
as likely to remain in DOS as anything that is not documented.
Second, while you can use the SET command to define variables
whose names have spaces in them or whose name begins with a
numeral, you cannot use %name% to access such variables.
What are global variables good for? I have placed today's date in
English (that is January 1, 1980 rather than 1-1-80) in my
environment at bootup and then I have a letter.bat which makes a
copy of a template with my address and my word processor's
formatting command and I append to this copy via
Key Stacking
in the BATch file. Two warnings are in order here. Be sure you
have a complete set of responses or else your system will hang.
In response to the "Are you sure", you must respond Y followed by
<Enter> so if you left the CR/LF out of yes.txt, "erase" would
patiently wait for the <Enter> and wouldn't take it from the
keyboard since you told it to only take input from yes.txt.
Second, you cannot redirect input and output to a BATch file as a
whole but you can redirect input or output of individual commands
if the commands use standard I/O. Thus, you cannot make a file,
2yes.txt, with two yes lines and a foo.bat with two lines saying
"erase *.*" and use "foo<2yes.txt". Foo will still insist on
input from you.
Summary
BATch commands provide the end user with a powerful set of programming
tools to develop custom and innovative solutions for everyday computer
needs.
///////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
////////////////// Remember where you saw this file first : \\\\\\\\\\\\\\\\\\
----------- ITRIS, the International Technology Research Institute -----------
\\\\\\\\\ http://www.fortunecity.com/skyscraper/data/567/index.html /////////
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////////////////