< Previous  |  Contents  |  Next >

11 : Batch Commands        


As outlined in the previous chapter, apart from all the DOS Plus commands examined so far, batch files have a simple language to allow automatic control of the way they execute. Some of these are simply to make life a little easier, while others are to allow more complex operations to be performed. The batch commands are listed below with a brief description, followed by a more detailed explanation of each.

      ECHO           Controls whether batch commands are displayed or not.
  GOTO   A command which makes control jump from one point in batch file execution to another.
  IF   A condition test in a file. The action taken can be controlled by the result of the test.
  REM   A reminder or remarks line in a program.
  PAUSE   Causes a delay in a program until a key is pressed.
  FOR   A loop operation, similar to 'FOR. .. NEXT' in BBC BASIC.
  SHIFT   Allows the limit of ten parameters to be extended.


Description:         ECHO is used to enable or disable the display of batch commands on the screen.
Syntax:   ECHO {ON | OFF | <S>}


This allows all batch commands to be displayed as they are executed, and is the initial setting.


Turns off the automatic display of batch commands.

ECHO Now insert backup disc

Displays the message "Now insert backup disc". Note that if ECHO is on, the message will be displayed twice ... once as the command itself is executed and again as a result of the command.


Displays the current status ECHO ON or ECHO OFF.


Unlike MS-DOS, the command ECHO. (ECHO full stop) does not generate a blank line, but just echos a full stop. Note that ECHO ignores any leading spaces from the text line, so

ECHO Normal Text
ECHO              Indented Text

would be displayed as

Normal Text
Indented Text

The command ECHO = behaves like ECHO without an argument, ie it displays the current ECHO setting. To display an = sign, the command ECHO == is used.


Description:         Alter the flow of control in a batch file.

GOTO <label>

The flow of the batch file may be changed by the use of GOTO and labels. A GOTO may be unconditional, or may depend on the result of an IF statement. A label consists of a colon (:) and a string of characters. Only the first eight characters of a label are significant.

When GOTO is encountered in the batch file, if the label is found, execution resumes at the line following the label. If the label is not found, the batch file terminates with an error message.


ECHO Init.bat not present on A: or B:
ECHO Setup complete

The above batch file SETUP.BAT will search drives A: and B: for the file INIT.BAT If not found the message "Init.bat not present on A: or B:" will be echoed. If found INIT will be called and the message "Setup complete" will be echoed to the screen.


Description:         A condition test.

IF (NOT) <condition> <command>

If the condition is true, then the conditional command is executed. Unless it is a GOTO the batch program continues execution on the next line. If the condition is false the conditional command is not executed and execution resumes on the next line.

NOT may be included in the expression, when the inverse of the condition test applies, ie if the expression is false, then execute the conditional command.

There are three possible forms of <condition>:


ERRORLEVEL <n> involves the use of an error code which is returned by a program to its caller. Thus, if a batch file calls a program, it may check the return code and, in this case, specify the highest error code allowed. The condition is true if the error code is greater than or equal to <n>.

2) <s1> == <s2>

<s1> == <s2> compares two character strings and is true if the strings are identical. Note also that this comparison does not ignore case – unlike most DOS Plus commands. For example "FILE"=="file" is not true.

3) EXIST <filespec>

EXIST <filespec> is used to check the existence of a file, allowing file-based actions to be carried out only if the specified file is found. This is probably the most common use. See GOTO for an example of the EXIST test.


Description:         A remark in the file which is totally ignored.

REM {<Optional remarks entered here>}

This command allows a remark to be inserted into the batch program. With ECHO ON, the remark line is displayed on the screen and this can be used to provide a commentary as the program is executed.

Example:   REM Remove all backup versions


Description:         Pause execution until a key is pressed.

PAUSE {<s>}

Programs sometimes need to wait for the user to perform some action, eg change a disc, or to give the user the opportunity to abort the batch program before a certain command is executed. PAUSE may be suffixed by a message string, and when executed will wait for a key press. The message string is non-functional, but may be seen when ECHO is ON.

When pause is executed the automatic prompt:

Strike any key when ready...

is displayed (whether ECHO is ON or OFF) and execution is suspended, pending a key press. If CTRL-C is pressed, the batch program can be aborted, so that a pause, with a suitable message displayed, is an ideal time to do this if such a decision is envisaged. If any other keys are used execution resumes.


Description:         Loop construct to perform multiple operations.

FOR %%<variable> IN (<set>) DO <command>

The program performs the command for each member of the subset <set>. The use of %%<variable> allows <command> to act upon the current member of the set. At any time the variable is set to the filename currently being processed. The double %% is used to distinguish between FOR variables and those used as parameters in the command line. If FOR is used manually from the DOS Plus prompt, rather than within a batch program, only one % is needed.

FOR statements cannot be nested. Any commands which can be included in a FOR construct could equally be carried out as separate commands, but FOR offers economy of effort.


FOR %%f IN (*.BAK) DO ERASE %%f

This is functionally equivalent to ERASE *.BAK


Copies each of the two files to A:


Sends all '.TXT' files and 'FIRST.HDR' to the network printer. Note that the choice of the %%variable names is purely convention. Any other variable name (not case sensitive) would do.


Description:         Re-assigns command line variables.


The SHIFT command allows more than ten command line arguments to be used. This is possible because more than ten can be entered with the original command, but only ten can be referenced directly. The SHIFT command moves all parameters to the left by one place, so that the previous contents %0 are replaced by the contents of %1, %1 contents are replaced by %2, and so on. The one beyond %9, which was previously inaccessible can now be referenced.

It should be noted that, after the first SHIFT, the identity of the current batch file name is lost. Another SHIFT loses the original %1, and so on. Items lost in this way are not recoverable.

The main use of SHIFT is to perform the same action repeatedly on a number of different objects, rather than to permit batch programs that are so general purpose that they require more than ten variables.


REM ARCOPY.BAT - Copies files listed to \archive
COPY %1 \archive\%1

To use this command, which copies any number of supplied filenames, you would type:

ARCOPY fred.txt bill.txt letter.wpx

and these files (or it might be a dozen) would be copied to directory \ARCHIVE directory.

< Previous  |  Contents  |  Next >

About the Master 512 | Bibliography