Classic Computer Magazine Archive COMPUTE! ISSUE 71 / APRIL 1986 / PAGE 83

Batch Files

Charles Brannon Program Editor

AmigaDOS is more than a console-driven disk operating system. By executing a sequence of AmigaDOS commands stored in a file, AmigaDOS takes on some of the characteristics of a programming language. Whether you want to simplify repetitive disk commands or create personalized custom commands, batch files further extend the range and flexibility of AmigaDOS.

No matter how easy it is to use a program, the most popular programs are those that give users more power. And although a program may have scads of powerful commands, the most powerful programs are those which let users put the commands together in new ways-in effect, to write programs.
    Instead of forcing you to always issue commands one at a time, a programmable application lets you create a script of commands to customize the behavior of the program. Whether we're talking about word processing macros, spreadsheet templates, relational database languages, or advanced machine language, programmability is the real key to software power. If you feel limited by a certain range of commands, you can combine the commands in new ways to create personalized features, just as we combine the vocabulary of English words to create a wealth of literature. Why just read when you can write?

Scripts, Sequences, And Batches
AmigaDOS is more than just a disk operating system-it's a programmable system that can process lists of its own commands as well as individual commands. In effect, AmigaDOS is a simple disk-oriented programming language.
    A list of AmigaDOS commands can be stored in a disk file variously known as a script, a sequence, or a batch file. The term "batch file" is most commonly used by those who work with PC-DOS, MS-DOS, and CP/M, which are also programmable disk operating systems. To keep things straight, we'll use "batch files" synonymously with "scripts" or "sequences."
    Even if you don't program in BASIC or any other language, you may be interested in learning about AmigaDOS batch files. The batch file "language" is simply made up of the same AmigaDOS commands you've probably been using all along (see "Introduction to AmigaDOS," Parts 1 and 2, in the February and March 1986 issues of COMPUTE!). There are also a few AmigaDOS commands designed especially for batch files.
    Creating and running batch files is easy. Using a text editor, you just type in a list of AmigaDOS commands. Then you save the list on disk under a filename. To run the batch file, you type EXECUTE filename at an AmigaDOS prompt. AmigaDOS reads the batch file and executes the list of commands, just as if you had typed them one by one yourself.
    We won't cover some of the more advanced features of batch files, useful only to advanced C and machine language programmers. Instead, we'll concentrate on the everyday utility of batch file programming.

A Quick Example
In a moment, we'll show how to create batch files with ED, the AmigaDOS full-screen text editor, but first there's a simpler way to create a short batch file. Enter this line at an AmigaDOS prompt:

copy * to Hello

(Note that AmigaDOS commands can be entered in uppercase or lowercase.)
    Although nothing seems to happen, AmigaDOS is waiting for you to enter some lines. We'll use the ECHO command to display a friendly message. ECHO displays any text that follows it within quotation marks, just like the PRINT statement in BASIC. One difference is that if you want to ECHO only a single word, the quotes aren't necessary.
    At an AmigaDOS prompt, enter the following text, pressing RETURN after each line:

echo "Hello!"
echo "I am your friend, the Amiga"
echo "personal computer."

    After the last line, press CTRL\. This key is the one to the left of the BACKSPACE key. CTRL-\ tells AmigaDOS that you're finished, and that it should finish writing and close the file. This key represents EOF, for End Of File.
    To confirm that you've typed the file correctly, enter:

TYPE Hello

You should see the same lines you typed. Now you can start this simple program:


This should print on the screen:
I am your friend, the Amiga
personal computer.

Using ED
It would be nice to have the Amiga actually speak this greeting. Rather than type in a whole new file, we'll use ED, the screen editor, to make the simple changes we're interested in. Enter:

ED Hello

    This runs ED and also loads the batch file named Hello. When you start ED, you can give it the name of any file to edit. If the filename doesn't exist, it will be created; otherwise the file is automatically displayed on the editor screen. (Incidentally, AmigaDOS has another text editor called EDIT, but it's not as easy to use as ED.)
    We'll make the Amiga speak the ECHO messages aloud by taking advantage of the system's built in speech synthesis via the AmigaDOS SAY command (added to AmigaDOS version 1.1). To learn more about SAY, just enter SAY by itself to enter an interactive mode with on-screen instructions.
    After you start ED by typing ED Hello, the batch file we previously entered should be on the screen, with the cursor at the beginning of the first line. ED is a fullscreen text editor, so you can move the cursor anywhere within the file (but not past the last line). To insert some text, just start typing. The DEL and BACKSPACE keys-can be used to delete characters.
    Move the cursor to the second ECHO line and press RETURN. This inserts a blank line. Cursor up to the blank line and enter:


You don't need to press RETURN at the end of the line, since you already did this to open up a line for typing.
    Now cursor to the end of the file and type:

SAY I am your friend, the Amiga
personal computer.

(Notice that SAY is the only AmigaDOS command that doesn't require you to enclose text containing spaces with quotes.) This is how your screen should look:

echo "Hello!"
say Hello!
echo "I am your friend, the Amiga"
echo "personal computer."
say I am your friend, the Amiga personal

    With the cursor at the end of the file, press the ESC key. An asterisk (*) should appear. Press the X key, then RETURN. This exits ED and saves your changes back to disk.
    Finally, type EXECUTE Hello to try out your talking batch file.
    Although these techniques are sufficient for simple editing, ED has dozens of editing commands. For example, CTRL-B (press CTRL and B at the same time) blanks out and deletes the line the cursor is on. ESC-J-RETURN joins two lines together. Space doesn't permit a discussion of all these commands, but if you like to experiment, refer to the abbreviated ED reference chart accompanying this article.

A special AmigaDOS batch file, called the startup-sequence, is executed automatically when you boot up an AmigaDOS or Workbench disk by inserting it at the Workbench prompt. Startup-sequence normally just displays a message, then launches the Workbench and ends the command line interface.
    To edit this batch file, enter:

ed s/startup-sequence

    This runs ED and calls up the file "startup-sequence" from the S subdirectory. This subdirectory, which can also be accessed as the S: device, is a convenient place for batch files. Just as AmigaDOS by default searches for AmigaDOS commands in the C subdirectory, the EXECUTE command first looks for a batch file in the S subdirectory. If AmigaDOS can't find the batch file in this subdirectory, it looks for it in the current directory. So no matter what your current directory is, you can always use your batch file if you place it in the S directory on your startup disk.
    When you first load startup-sequence into ED, you'll see something like this:

Common ED Commands

Immediate Commands (hold down CTRL and press key):

CTRL-A  Insert line at cursor position
CTRL-B  Delete current line
CTRL-D  Scroll text downward
CTRL-E   Move cursor to top or bottom of screen
CTRL-N  Delete character at cursor
CTRL-O  Delete word or series of spaces
CTRL-U  Scroll text upward
CTRL-Y  Delete to end of current line

Extended Commands (precede by pressing and releasing ESC):

Move cursor to bottom of file
E/stringl/string2/ Exchange stringl to string2
EQ/stringl/string2/ Exchange, but query first
F/string/  Find string
Join current line with next line
Quit without saving text
Move cursor to top of file
Exit, save text

echo "Workbench disk. Release 1.1"
echo " "
echo "Use Preferences tool to set date"
echo " "
endcli > nil:

    Since this message appears every time you start up your disk, you may want to change the ECHO statements for a personalized message. Likewise, if you'd rather use AmigaDOS instead of the Workbench, delete the last two lines. The "> nil:" sequence makes AmigaDOS throw away the output of a command; here, the message "CLI task 1 ending."
    Startup-sequence is a good place to put personalized commands. For example, if you like to keep your command directory in RAM for speed and convenience, you could insert these lines above the LoadWb line:

makedir ram:c
copy c to ram:c all quiet
cd ram:c

    This copies all of the AmigaDOS commands from the C subdirectory on the floppy disk into a C subdirectory on the RAM disk. It also changes the current directory to the C subdirectory in RAM:, so any AmigaDOS commands you type from then on will be loaded from RAM: instead of from the floppy. In effect, this turns AmigaDOS into a memory-resident DOS, with all commands intrinsic instead of extrinsic. AmigaDOS responds much faster this way. However, this also uses up quite a bit of memory, so you may want to copy only the commands you use frequently.
    Another useful startup action is to set the date and time. You can always do this with the Preferences tool or by opening a CLI and using the DATE command. However, it can be more convenient to enter the date when you first turn on your Amiga, allowing all files subsequently saved to be stamped with the current date and time. Just insert this line into startup-sequence:

date ?

    The ? operator can be used in place of the parameter of a command. Instead of specifying the date, ? prompts the user to enter the date. It also displays the template for the date command (TIME,DATE, TO=VER/K:). If you like, use ECHO to display your own prompt, and > nil: to discard the template:

echo "Please enter the date and time."
date > nil: ?

From then on, whenever you boot up from this disk, you'll respond to the prompt by typing something like this:

27-jan-86 15:12

which automatically sets the system clock.

Variable Parameters
You can also send special options to your batch file. You enter these options on the command line along with the EXECUTE command. Just as with variables in BASIC, you can manipulate these parameters symbolically.
    Let's say you'd like a batch file that gives you complete information on a file. It uses LIST to display the information about the file, and TYPE to display the file. You would use a command like EXECUTE SHOW RODEO to display the file RODEO. Use ED SHOW or COPY " TO SHOW to create this batch file:

.KEY name
LIST <name>
TYPE <name>

    .KEY (don't forget the leading period) sets up a name for substitution text. Whatever you typed on the same line with EXECUTE is substituted wherever you use <name>. You must use the angular brackets, or LIST and TYPE would look literally for the file "name."
    After creating this batch file, type this at an AmigaDOS prompt:

The result is the same as if you had typed LIST S/STARTUP-SEQUENCE followed by TYPE S/STARTUP-SEQUENCE.
    Other AmigaDOS commands let you check to see if the user has entered a specific string and check to see if a file exists. To prevent an error message, we can check to see if the file exists before we use LIST and TYPE:

.KEY name
IF EXISTS <name>
LIST <name>
TYPE <name>
ECHO "<name> does not exist!"

    Notice the use of IF, ELSE, and ENDIF. Looks like Amiga BASIC, doesn't it? In fact, the AmigaDOS IF-ELSE-END IF commands function very much like BASIC's. When the IF condition is true, AmigaDOS executes the following statements; otherwise the following statements are ignored. ELSE executes the statements following it only if the preceding IF was false. ENDIF cancels conditional processing and returns to executing all commands.

Any Parameters Missing?
Here's how to use the IF EQ option to test for the existence of a command-line parameter. If there is no parameter, <name> is null, so "<name>z" is simply "z'". We use NOT to reverse the test. If the parameter "<name>z" is NOT equal to "z", then we must have a command line parameter. (We can't just test IF <name> NOT EQ "", since EQ wants two parameters, and the null string "" is not a parameter, but the lack of one.)

.KEY name
IF <name>z NOT EQ z
LIST <name>
TYPE <name>
ECHO "You didn't give me anything to SHOW."

    Although you can't use leading spaces in the actual batch file, it's easier to follow the IF-ENDIF structures when you use indentation. Just don't type in the leading spaces. This version of the batch file SHOW checks both for the existence of the filename and for the presence of the filename parameter:

.KEY name
IF <name>z NOT EQ z
    IF EXISTS <name>
      LIST <name>
      TYPE <name>
      ECHO "<name> does not exist!"
   ECHO "You didn't give me anything
   to SHOW."

    You can use more than one parameter in the KEY statement, just as many commands, such as DATE, accept two inputs.
    If the user doesn't enter anything for the parameter, you can assign a default value using either .DEF or $. If you use DEF, the default phrase is used throughout the batch file. In this example, SHOW displays itself if you don't give it a filename.

.KEY name
.DEF s/show
LIST <name>
TYPE <name>

    You can use $ to substitute a default value only for the current substitution. Several batch commands may use the value in different ways, so each command may have its own default value. In the following example, LIST displays the whole directory if <name> is null, but TYPE types the file "TEMP" if <name> is null:

.KEY name
LIST <name>
TYPE <name$temp>

Labels And Branching
You can jump forward to a label with the SKIP command. You'd typically use SKIP along with an IF condition if you want to skip over a block of statements that shouldn't be executed if the IF was true. You declare the label with LAB. SKIP can't skip backward, only forward to a LAB statement. You can usually use IF and ELSE to accomplish the same thing, though.

.KEY name
IF exists <name>
TYPE <name>
ECHO "<name> doesn't exist."
echo "Finished."

    An EXECUTE command can execute another batch file, or even itself. This permits backward loop ing to some degree. Nested batch files can be quite handy. You can test and debug individual batch programs, then execute them together from a master execute script:

EXECUTE Greeting
EXECUTE Assignments
    The individual files could themselves contain other EXECUTE references.

ASSIGNIng Shortcuts
If you're using EXECUTE a lot, you may grow weary of typing it. You can always rename EXECUTE to something short like x, but other batch programs may contain EXECUTE statements, requiring you to rename it back. Instead, you can use the ASSIGN command to assign any filename to a device name.


    You can now use x: whenever you want to use the EXECUTE command. (The prefix sys:c/ makes sure that EXECUTE can be found no matter what directory you're in.)
    The device name you create should not conflict with an existing one. To get a list of the current assignments, just type ASSIGN. You may want to ASSIGN d: c:list for a convenient and quick shorthand for directories (c: is synonymous with the C directory). You can then just type d: to get a LIST.
    ASSIGN can be so handy for this kind of thing that you'll probably want to include your own sequence of ASSIGN commands within startup-sequence. If you put your ASSIGN statement within startup-sequence, you'll get these assignments for every session. Just remember that ASSIGN can only be used to attach a device name to a particular filename. ASSIGN d: "c:list quick" doesn't seem to work. Although LIST is a filename in the c directory, the "quick" parameter is not part of the filename.