home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Big Blue Disk 11
/
bbd11.zip
/
BITS&PCS.TXT
< prev
next >
Wrap
Text File
|
1987-06-11
|
8KB
|
146 lines
|D╔══════════════════╗════════════════════════════════════════════════════════════
|D║ |5The Happy Hacker |D║════════════════════════════════════════════════════════════
|D╚══════════════════╝════════════════════════════════════════════════════════════
^C|0▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
^C|0█|E~0Bits 'N PC's ^0|0█
^C|0█|F~0 by ^0|0█
^C|0█|F~0Daniel Tobias^0|0█
^C|0▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
This month's topic is batch files. Through the use of these, you can make
your PC do many repetitive tasks automatically.
A batch file is basically a list of commands for DOS to execute. The name
"batch" derives from the old days of computing, when all commands to computers
had to be given on stacks of punchcards. This was known as "batch-processing"
mode, since the system operators gave the computer its tasks in batches, which
it churned through and ultimately returned its result on printouts.
In modern times, the interactive mode has become more popular. In this
mode, you give commands to the computer via keyboards, mice, light pens,
joysticks, etc., and they are executed right away. This is much more convenient
for the user in most cases. However, there are still times when you have a
predefined task that must be done frequently, and you'd like to just tell the
computer to start it up, without the user needing to intervene and enter every
single required command in the proper sequence.
That is why the designers of MS-DOS provided the capability of "Batch
Files." Like the old stacks of punchcards, a batch file contains a sequence of
commands to be executed. However, MS-DOS also provides such advanced features
as branching and conditional expressions. In fact, MS-DOS batch files can be
considered to be a programming language in their own right, and you can do many
powerful things with them.
A batch file is an ordinary ASCII text file, given a filename with the
extension ".BAT". You can use any text editor (such as BlueLine) to create
batch files. Each line of a batch file, terminated by a carriage return,
contains one DOS command. Any legal DOS command, or invocation of any program,
can be used in a batch file. In addition, several special commands have been
created specifically for batch file use. Here are some of them:
^1REM^0 indicates a comment line that is not executed. Such lines are ignored.
^1PAUSE^0 makes the PC stop and wait for the user to press a key. The prompt
"Strike a key when ready..." is displayed.
^1CLS^0 clears the screen.
^1ECHO^0 has several functions. ^1ECHO OFF^0 causes DOS to stop showing the commands
as it executes them, allowing the batch file to proceed invisibly. (This just
stops output of the command lines executed; any output produced by the commands
and programs invoked by the batch file is still shown.) ^1ECHO ON^0 "undoes" ^1ECHO
^1OFF^0, restoring output of each line executed. ^1ECHO^0 followed by any other
text causes that text to be output.
^1GOTO^0 causes a branch to a different part of a batch file. Define labels to
show DOS where to go by entering a line whose first character is a colon (:),
followed by a label name (such as "line1" or "loop"). ^1GOTO LINE1^0 would cause
execution of the batch file to continue at LINE1 instead of sequentially. Use
GOTO along with the IF command (described below) for conditional branching.
^1IF^0 is the most powerful batch command. Its syntax is as follows:
^1IF <condition> <command>^0. <Command> is any command allowed in a batch file.
It is executed only if <condition> is true. <Condition> is one of the following
three things:
^1EXIST <filename>^0: This is true if a file exists by the name of <filename>.
^1<string1> == <string2>^0: This is true if the two strings are identical. This
becomes useful when you use parameters, described below.
^1errorlevel <number>^0: This checks the errorlevel returned by a command or
program that was just executed, and is true if this level (a number from 0 to
255) is greater than or equal to the given <number>. Some programming
languages allow you to have a program terminate with a particular errorlevel.
For example, in Turbo Pascal, you can end a program with ^1halt(32)^0 to cause an
errorlevel of 32 to be sent to DOS. This can be used to control execution of a
batch file.
In addition, there is the form ^1IF NOT <condition> <command>^0 which executes
<command> only if <condition> is NOT true.
^1FOR^0 allows commands to be repeated. It is another powerful, complex
command. Its syntax is ^1FOR <dummy variable> IN <set> DO <command>^0. The dummy
variable consists of two percent signs followed by a letter, like "%%x". <Set>
is a list of parameters (usually filenames) surrounded by parentheses and
separated by spaces, like ^1(go.bat present.chn pasrun.com return.chn)^0. Finally,
<command> is any DOS command. If you include the dummy variable name anywhere
in the <command>, it will be replaced in turn with each element of the <set>.
Thus, ^1FOR %%A IN (THIS.TXT THAT.TXT ANOTHER.TXT) DO COPY %%A B:^0 will copy the
files THIS.TXT, THAT.TXT, and ANOTHER.TXT to drive B.
A powerful feature of batch files is the ability to use parameters. When
you invoke a batch file by typing its name (with or without the .BAT extension),
you can follow it with a list of parameters separated by spaces. They can be
inserted into any command within the batch file by using a percent sign (%)
followed by the number of the parameter: %1 for the first parameter, %2 for
the second, and so on. Hence, if you invoke JUNK.BAT with the command:
^1JUNK 1 2 3 FOUR^0, then the sequence %1 anywhere in the file will be replaced
by the string "1", and similarly %2 is replaced with "2", %3 with "3", and %4
with "FOUR". %0 represents the name of the batch file itself. Since the %
sign is a special character in batch files, if you actually want "%" to be used
in a command, it must be typed twice, as "%%". This is why %% is used in dummy
variable names in the FOR command; if you type a FOR command in direct mode
rather than a batch file, you would just use one percent sign.
The ^1SHIFT^0 command causes parameters to be "shifted" over one. After SHIFT
is executed, the old parameter %1 becomes %0, while %2 becomes %1, and so on.
This allows more than nine parameters to be used; the tenth parameter will be
shifted to %9. Doing repeated SHIFTs will bring more parameters to accessible
numbers. The original value of %0 is lost after SHIFT is executed.
Here's an example of a batch file using some of these features:
^1 ECHO OFF
^1 IF EXIST %1 GOTO OK
^1 ECHO That file is not found.
^1 GOTO END
^1 :OK
^1 IF %2 == D DEL %1
^1 IF %2 == C COPY %1 B:
^1 FOR %%A IN (%3.TXT %3.DOC %3.HLP) DO DEL %%A
^1 ECHO Done!
^1 :END
^1 REM End of batch file.
This batch file takes three parameters. The first is the name of a file. If
no such file exists, the batch file terminates, giving a message that the file
was not found. Otherwise, the second parameter is examined; if it is the
letter D, then the file is deleted. If it is the letter C, then it is copied
to drive B. After this, the FOR command is used to delete all files with names
beginning with the third parameter, and ending with the .TXT, .DOC, and .HLP
extensions. Then a message "Done!" is output, and the file ends.
So, if the above file was named STUFF.BAT, then typing:
^C^1STUFF JUNK.TXT D STUFF^0
will cause JUNK.TXT, STUFF.TXT, STUFF.DOC, and STUFF.HLP to be deleted (assuming
all these files exist to begin with.)
Examine DO.BAT, a batch file on this disk, for a good example of things you
can do with a batch file. You might also check our GO.BAT (used to start up
BIG BLUE DISK), and any other batch files you may encounter, for more examples
of batch file usage.