The magazine of the Melbourne PC User Group

Another Innings with BAT
Stephen Davey

Some more simple techniques to make your batch files more useful and effective. (This article is the second of a series written mainly for beginners. The first, dealing with basic batch files and path commands, was published in PC Update, November 1988)

In the previous article we set up a very basic menu system using a series of simple batch files. This system, while effective, can become a little cumbersome, as each item added to the menu needs its own individual batch file. Let's look at a few more batch file commands that we can use to streamline a menu system or some other procedure using a batch file.

Variable (%)

Sometimes you might want to pass a variable (or replaceable) parameter to a batch file. For example if we set up a batch file (called say DB.BAT) to delete all backup (*.BAK) files on drive A, it might look like this:

REM This file deletes *.BAK 
A:DEL *.BAK


However, this batch file only deletes the files from drive A:. If we want to delete backup files from other drives we would need to make a separate batch file for each, or, we could replace the 'A' in the batch file with '%' we can choose whatever drive we want The file would now look like this:

REM This file deletes *.BAK 
%1: 
DEL *.BAK


Now, if instead of just typing 'DB' we type 'DB B' the 'B' (or any variable) after the 'DB' will replace the '% 1' within the batch file. More than one variable can be used. For example, we could include a directory path in our 'DB' file:

REM This file deletes *.bak 
%1: 
CD %1:\%2 
DEL *. BAK


If we now type "DB C LETTERS", "C" replaces "%1" and "LETTERS" replaces "%2" within the file so that the following will happen:

REM This file deletes *.BAK 
C: 
CD C:\LETTER
DEL *.BAK


Note that any variables typed after the name of a batch file must have a space between them and will be automatically be designated % 1, %2, %3 etc, depending on their position after the file name. Within the file there is no restriction on which variable is used first; or if in fact all the variables are used.

Getting into GOTO

The GOTO command transfers control to the line following the appropriate "label". For instance in the following file the second and third lines will be jumped as control skips to the label "END":

GOTO END 
REM This line will be skipped 
REM And also this line 
:END 
REM The batch file continues here.


Note that the label can be any character string where the first eight characters are significant. The label name following the GOTO command is listed without any colon (:) but the actual label is preceded by a colon.

And IF (NOT)

The IF command allows certain batch file commands to be carried out only "if" (or "if not" some other condition is true, or false. The format for this command is: IF [condition] [command], or IF NOT [condition] [command]

The [condition] parameter is one of the following:

STRING 1 == STRING 2, or 
EXIST Filename, or 
ERRORLEVEL Number


If the condition is true, then the DOS [command] following is executed. Otherwise the DOS command is skipped and the next command in the file is executed. The [command] can be a batch file command such as GOTO or ECHO, or a program command such as WORD etc.

IF STRING 1 == STRING 2

We can use this IF command to test, for example, whether the variable we entered after a batch file name is a correct request. With our DB.BAT example file, we know that only disk drives available are, say A:, B: and C:. We can get the batch file to issue a warning if we enter an incorrect drive. To do this we need to use both the IF and GOTO commands. By adding the following lines at the start of the batch file we can check the first variable (ie the disk drive):

IF %1 == A GOTO START 
IF %1 == B GOTO START 
IF %1 == C GOTO START 
ECHO Incorrect Drive Requested 
GOTO END 
:START 
REM This file deletes *.BAK 
%1: 
CD %1:\%2 
DEL *.BAK 
:END


Here, the first three lines check to see if the first variable equals A:, B: or C:, and if so goes to the START label, therefore avoiding the fourth and fifth lines of the batch file. If the variable is not one of the three acceptable, then processing will go to the fourth line and display the text "Incorrect Drive Requested". Then the main part of the batch file will be avoided by the next, GOTO END, line.

IF EXIST Filename

The following line will check to see if a file named "LETTER.DOC" exists, and if it does then proceed to the label "NEXT":

IF EXIST A:LETTER.DOC GOTO NEXT

The reverse would be:

IF NOT EXIST A:LETTER.DOC GOTO NEXT

(We will discuss EXIST and ERRORLEVEL conditions in more detail later.)

Now, back to the menu

Let's see how we can now tidy up our menu system. Rather than having one batch file to display the menu screen and one batch file for each item on the menu, we can now include them all in the one file.

To make things easy later, we'll call this new menu batch file M.BAT, and it might look like this: (For ease of discussion later each line has been numbered. The number and brackets would not be included in any real batch file.)

( 1) ECHO OFF 
( 2) IF %1blank== blank GOTO SCREEN
( 3) GOTO %1 
( 4) :SCREEN 
( 5) CLS 
( 6) ECHO ********** 
( 7) ECHO * MENU 
( 8) ECHO *
( 9) ECHO * Wordprocess M W
(10) ECHO * Spreadsheet M S
(11) ECHO * Ventura M V
(12) ECHO *
(13) ECHO * Press required
(14) ECHO * letters 
(15) ECHO ************ 
(16) GOTO END 
(17) :W 
(18) C: 
(19) CD \WORD\DOCUMENT
(20) WORD
(21) M
(22) :S
(23) C:
(24) CD \SPREAD
(25) PC CALC
(26) M
(27) :V
(28) C:
(29) CD \VENTURA
(30) VP
(31) M
(32):END

To run this batch file we can either just type "M" (the name of the file) in which case the menu screen will be displayed, then returning us to the DOS prompt so that we can make a choice (or insert any other DOS command).

If we can remember the choice, we can shortcut the menu screen by typing "M", followed by a space and then the letter required. This is how it works:

Line 1 turns the echo off so that only the lines we want are displayed (echoed) on the screen. Line 2 checks to see if a variable (%1) has been entered after the "M". If no variable was entered then "% 1blank" will equal "blank" and so line 3 will be missed as we go to the SCREEN label on line 4.

If a variable was entered, then "% 1blank" will not equal "blank" and so we will proceed to line 3. Line 3 simply tells the batch file to go to the label that has the same name (or letter) as the variable that was entered after the batch file name. For instance, if we typed 'M W' then %1=W and we would go to the :W label, thus bypassing the menu screen display.

Line 4 is the label at the start of the screen display and line 5 just clears the screen in readiness for the menu screen. Lines 6 to 15 make up the screen display and can be elaborated as you like, but make sure you do not use more lines than your screen has depth, or the first few lines will scroll off the screen. Note also, that with this method, no ECHO line can be blank. (See alternative menu display section later). After the menu screen has been displayed Line 16 directs us to the end of the batch file and therefore the DOS prompt. The menu screen will still be displayed on the screen and we can now enter our choice (or a DOS command).

If we want to use Ventura we enter "M V". In effect this is just the name of the batch file and the variable "V". Be sure to put a space between the M and the V.

(If we had called this file MENU.BAT, we would have to enter "MENU V" - more of a mouthful!)

Now, as the file proceeds, when we get to line 3 we will GOTO %1. As %1=V we will go to the label : V on line 27. Lines 28 to 30 then simply start the Ventura program for us. When we exit from Ventura we will automatically return to Line 31, which restarts the M.BAT file and displays the menu screen and the waiting DOS prompt ready for the next choice.

Alternative Screen Displays

Using the ECHO command to make up your screen display can have its disadvantages - such as no blank lines or ANSI characters. An alternative method is to make the screen display as a separate text file called, say, MENU.TXT. This file would then be displayed by replacing Lines 6 - 15 with the single line:

TYPE MENU.TXT

Remember to include the correct path for the MENU.TEXT file. This file could be created by any word processor or graphics program that will allow the file to be displayed on the screen using the DOS TYPE command. The program THEDRAW, available on the BBS, is excellent for this, as very fancy flashing, moving displays etc can be produced.

More about EXIST

Suppose that we do not have a hard disk but instead a two-disk system. To use the word processing program we would have to insert the particular program disk in one of the drives. We could just get the batch file to stop and request us to insert the correct disk by using the following two lines:

ECHO Insert correct program disk in drive B: 
PAUSE


This procedure would not however actually check to see if we had in fact put the correct disk in the drive, and also wastes time if the correct disk was already in the drive. We know that the correct disk should have the word processing program command file available. With MS-Word this file is called WORD.COM (Check your particular program)

:START 
A: 
WORD 
etc


Here, the first line is just the label we have used to identify the checking part of the batch file, the second line hunts for the WORD.COM file on drive A and if found then proceeds to the :START label to run the program. If the file is not found then the "Insert correct etc" line is displayed followed by the "Strike any key when ready etc" line produced by the PAUSE command. When a disk is inserted and a key pressed, the file goes back to the :CHECK label to hunt again for the correct file. (Note that this will make an endless loop if the correct disk can not be found; so it may be wise to put in an escape clause. After the first echo line insert the following line:

ECHO Press Control C to Exit

By pressing Control C you will be given the choice to terminate the batch file.

More about ERRORLEVEL

There are some useful utility programs that take your keyboard entry and interpret it as a DOS error level. We can then test for this error level in our batch file so that we can then direct the processing to a specific label. One good example is a program called INPUT.COM, available from the BBS. When included in a batch file, this program can be used to display a line of text, then set the DOS errorlevel to what ever number you type in. We can then use the IF ERRORLEVEL command to check and then GOTO particular labels.

Taking our simple menu file as an example, we could use it in this way:

CLS 
ECHO ********************
ECHO * MENU
ECHO *
ECHO * WORDPROCESS 1
ECHO * SPREADSHEET  2
ECHO * VENTURA         3 
ECHO *
ECHO * DOS               0
ECHO *
ECHO ********************
INPUT Press required number 
IF ERRORLEVEL 3 GOTO VENT 
IF ERRORLEVEL 2 GOTO SPRED
IF ERRORLEVEL 1 GOTO WORD 
GOTO END 
:WORD 
CD \WORD\DOCUMENT 
WORD 

:SPREAD 
C: 
CD \SPREAD 
PC-CALC 

:VENT 
C: 
CD \VENTURA 
VP 

:END

In this menu batch file, a menu screen is displayed using the ECHO command. You will notice however, that the choices are now numbers and not letters. When the INPUT command is found, the line of text is displayed and the system waits until a single number is typed (INPUT accepts only the digits 0-9 and ignores any second digit or the enter key). Whatever number was pressed now becomes the DOS errorlevel and the next few lines check to find out if the errorlevel matches those required (in this case 1, 2 or 3). If some other number has been entered the batch file will pass through each of the ERRORLEVEL checks and then GOTO END (ie back to DOS). The main disadvantage of this type of menu.batch file is that the file will only accept an input of the number 0 to 9. If we want to use a DOS command instead of one of the menu choices, we will have to first exit to DOS, then enter the DOS command. With our previous example (using 'M W' for example), we could enter either a menu choice OR a DOS command. Another similar program to INPUT is QUERY (Melb 038) which stops the batch file processing and waits for either the character 'Y' or 'N' and then sets the DOS errorlevel. This is particularly useful and allows you to create batch file procedures that for example allow you choose to continue or escape back to DOS etc.

Reprinted from the Jan - Feb 1989 issue of PC Update, the magazine of Melbourne PC User Group, Australia

[About Melbourne PC User Group]