Classic Computer Magazine Archive COMPUTE! ISSUE 65 / OCTOBER 1985 / PAGE 86

All About
IBM Batch Files
Part 2

G. Russ Davies

Part 1 of this article (COMPUTE!, September 1985) covered the fundamentals of batch programming on the IBM PC/PCjr. This month we'll look at some advanced techniques and a utility that makes batch programs interactive and easier to use.

As we saw last month, IBM batch programs can be very powerful. The batch commands FOR, IF, and GOTO permit program loops, conditional tests, and program branching. You can also chain two or more batch programs together and pass information from one to another.
    But batch programs have limitations, too. Visual displays are often unexciting, consisting of single color alphanumerics (no graphics characters, etc.), and user input is even more restricted. The PAUSE command allows only two options: continuing after the pause or ending the program. This virtually rules out complex, interactive programs that let you select from several different options to perform various tasks.

Adding Choices
The "CHOOSE.COM" program below provides the equivalent of a new batch command. As the name suggests, CHOOSE lets you make a choice. It can be used by itself to request a yes/no response, or with additional information to offer several different options. Since CHOOSE.COM is a machine language program, we've included a BASIC filemaker program that creates it for you. Type in and save Program 1 as listed below, then run it. Once that's done, you can try out the simpler "yes/no" form of CHOOSE.
    Remember from Part 1 that any batch program named AUTOEXEC.BAT loads and runs automatically when you boot the system. An AUTOEXEC.BAT program that doesn't include the DOS commands DATE and TIME won't prompt you to enter the date and time (as normally happens when you boot up). Though it's often valuable to have correct date and time information on new files, there are also many times when you don't need it.
    The short batch program that follows lets you choose whether to add date and time settings. Enter it as listed, using the EDLIN program (on the DOS Supplemental Programs disk) or any word processor or text editor that produces standard ASCII output. Since this and the following examples are not BASIC programs, don't try to enter them with COMPUTE!'s "IBM Automatic Proofreader." Once you have entered this program, save it with the filename AUTOEXEC.BAT. Because the program calls CHOOSE.COM, you must save it on a disk that contains CHOOSE.COM.

echo off
  echo Do you wish to set the date/time?
  rem press Y,y,N, or n to answer
  goto :next


    After saving this program, run it by rebooting the system (press Ctrl-Alt-Del or enter AUTOEXEC). When used without parameters, CHOOSE checks for a yes/no response, permitting uppercase as well as lowercase Y and N (it's not necessary to press the Enter key after typing Y or N). Other responses (except Ctrl-Break) cause the prompt message to be displayed until a valid choice is made.

After you respond with yes or no, CHOOSE passes this information to the batch program via ERRORLEVEL. As explained in Part 1, ERRORLEVEL is a special variable you can test with IF. In this example, CHOOSE sets ERRORLEVEL to 1 when the response is yes, and 0 when the response is no. The GOTO command then branches appropriately. Note that GOTO branches to a destination label, which is a colon followed by a string. This program uses the labels :setdt and :next. Don't confuse the label :next with BASIC's NEXT statement (which doesn't exist in batch programming).
    In this case, ERRORLEVEL can have only one of two possible values, but it can take higher values as well (see below). When testing ERRORLEVEL with IF, keep in mind that the IF ERRORLEVEL statement is true when ERRORLEVEL is greater or equal to the number being tested. If you tested for 0 first in this program, ERRORLEVEL would always be 0 (1 and 0 are both greater than or equal to 0). When testing ERRORLEVEL, you must always test for higher values before testing for lower ones.

Multiple Options
Most utility programs offer a variety of options. Typically, they display a menu with a list of options, and you choose the option you want by pressing a certain key. CHOOSE makes it easy to present such menus within a batch program. First display the options on the screen, then use CHOOSE followed by a list of the keys you wish to test. For instance, the statement CHOOSE ABC checks the A, B, and C keys and returns appropriate values in ERRORLEVEL. The ERRORLEVEL value corresponds to the position of the key in the list after the CHOOSE command. Thus, after the program performs CHOOSE ABC, ERRORLEVEL equals 1 if A was pressed, 2 if B was pressed, and so on.
    When using CHOOSE with several option keys, it's critical to list the keys in the right order. Since you must always test for higher ERRORLEVEL values before testing for lower ones, you'll want to put the most likely (or most speedcritical) options at the end of the option key list. This assigns higher ERRORLEVEL values to the more important options.

Entering FILES.BAT
The "FILES.BAT" program below demonstrates multiple-option selection as well as a colorful, attractively formatted menu and help panel. It sorts any disk directory by file size, date, filename extension, or alphabetical order, and can also create separate batch files for mass DOS operations. Entering the program requires several steps:

1. Make sure your disk contains the system file called ANSI.SYS. If necessary, copy ANSI.SYS from the DOS disk with the COPY command. This file contains the screen/keyboard driver used for graphics displays and temporary key assignments.

2. Make sure your disk contains a file named CONFIG.SYS that includes the statement DEVICE=ANSI.SYS. If your disk already has a CONFIG.SYS file, add that statement to the file with EDLIN or another text editor. If your disk doesn't have a CONFIG.SYS file, create one by entering these lines:


Next press the F6 key to end the file, then press Enter. Your disk now contains the necessary CONFIG.SYS file.

3. Using EDLIN or some other text editor, enter Program 2 as listed below and save it on disk with the name FILES.BAT. (Since this is not a BASIC program, don't try to enter it with the IBM Automatic Proofreader.) Several lines in the listing contain the characters {CTRL-P}. The braces indicate that this is a special control character which you must enter by pressing a combination of keys. Do not type the braces. Instead, wherever you see {CTRL-P} in the listing, hold down the Ctrl key and press the P key. On the screen, you'll see the wedge-shaped control character that precedes special ANSI. SYS screen or keyboard instructions. Type everything else in Program 2 exactly as it appears.

4. In the same manner, enter Program 3 as listed and save it on disk with the name FILES.MNU (do not use any other filename). This file is graphics data for the menu. Whenever you see {CTRL-P} in the listing, enter CTRL-P as described in step 3. A number enclosed in braces indicates a graphics character (the number is an ASCII code) which you must enter with the Altkeypad technique on the PC and by another method on the PCjr. For instance, where the listing contains {218}, hold down the Alt key, then type the characters 2, 1, and 8 on the numeric keypad. When you release the Alt key, character 218 appears on the screen. On the PCjr, hold down Alt, press Function-N, then enter the numbers as on the PC. After all three numbers are entered, release the Alt key; the character will appear on the screen. When the braces enclose two numbers, several characters are needed; the first value shows how many characters to enter, and the second is the ASCII code. For instance, where you see {5 196}, use the above procedure to enter character 196 five times. Where you see the letters SP followed by a number and enclosed in braces, you should type the space bar the indicated number of times. For example, {SP 16} means to type 16 spaces.

5. Enter Program 4 as listed, using the technique described for step 4, and save it on disk with the filename FILES.HLP (don't use any other filename). This file contains graphics data for the Help screen.

6. Enter a batch program that contains nothing but a REM statement and save it on disk with the filename QUIT.BAT. This can be done with a text editor or by entering these statements from DOS:


Now press the F6 key followed by Enter.

7. Activate BASIC and type in Program 5. Since this program is listed in BASIC, enter and save it using the IBM Automatic Proofreader published bimonthly in COMPUTE!. You must save this program with the filename FILEGRP.BAS.

8. Finally, before using FILES.BAT, check your disk to make sure all the necessary files are present. It must contain CHOOSE.COM, ANSI.SYS, CONFIG.SYS, FILES.BAT, FILES.HLP, FILES.MNU, FILEGRP.BAS, and QUIT.BAT. The program will not work correctly unless all these files are on one disk and named as shown here. Note that the FILEGRP option (see below) also requires BASIC.

Before you run this program, reboot the system by turning the computer off and on or by pressing Ctrl-Alt-Del. This guarantees that the ANSI.SYS driver is present. To run FILES.BAT, enter FILES after the DOS prompt and press Enter. Most of the program is self-explanatory - after all, that's what menus and help screens are for-so we won't describe every option.
    The FILEGRP option lets you create a separate batch file (named FILEGRP.BAT) for performing op erations on a group of files. Every line in FILEGRP.BAT consists of a filename from the subject disk and four dummy parameters in this order:

%1 filename.extension %2 %3 %4

    Dummy parameters are replaced by actual parameters you supply when running FILEGRP.BAT. This makes it easy to perform the same operation (copy, print, delete, etc.) on a large group of files. After using the FILEGRP option, exit to DOS and use a word processor or text editor to edit FILEGRP.BAT as needed, deleting the names of any files you don't want to include in the operation. Then run FILEGRP.BAT by entering its name followed by the needed parameters. The first parameter can be any DOS command; the rest will be parameters that are relevant to that command. For instance, you might enter FILEGRP COPY B: /V to copy the files listed in FILEGRP.BAT onto drive B. Incidentally, BASIC does not provide any way to set ERRORLEVEL.

Advanced Batch Programming
FILES.BAT employs several techniques you may find useful. The DOS command BREAK ON makes the system respond to Ctrl-Break in more instances than normal. The TYPE command is used to display graphics like the menu and help screen. TYPE creates such displays much faster than the DOS ECHO command (you could also use COPY).
    The ANSI.SYS driver assigns the lowercase keys a, s, d, e, b, and i to their uppercase equivalents to reduce the amount of testing required. The F1 and F10 keys are assigned to keys H and X, respectively, so those function keys perform their usual HELP and EXIT roles. After CHOOSE accepts a response, the modified keys are restored to their original definitions. Pressing Ctrl-Break while CHOOSE is active (or pressing Y in response to "Terminate batch file?") leaves these keys reassigned. To avoid this effect, you should normally exit by pressing F10.
    The F10 (EXIT) function uses a trick to perform a quick exit. It simply runs QUIT.BAT, a batch program that consists of a do-nothing REM statement. When any batch program ends, it ends all preceding batch programs as well. Note that since ECHO OFF is in effect when QUIT.BAT is called, the REM is not displayed.
    Batch commands are not particularly fast. To optimize speed, structure the program so that the most-often used (or speed-critical) routines are closest to the place you're branching from. The fewer program lines that a GOTO has to skip over, the quicker it executes. You can also speed up batch programs by using extra disk buffers as explained in the DOS Manual. REM statements slow batch programs drastically; if you want to document the program, store your comments in a separate file.
    In some cases it's useful to test for the absence of a parameter. For instance, you might want to reprompt the user with a message like "You must enter more information." This can be done with a statement such as IF .--%l. GOTO.NOPARM. This line means "if a dot equals the parameter plus a dot then go to the no-parameter routine." The IF test is true only when no parameters have been entered.

Program 1: CHOOSE.COM

For instructions on entering this listing, please refer to "COMPUTE!': Guide to Typing In Programs" published bimonthly in COMPUTE!.

       PUT AS #1
LA 110 READ X$:IF X$="/*" GOTO 1
CA 120 PRINT #1,CHR$(VAL("&H"+X$
       ));:GOTO 110
KB 140  DATA A0,80,0,3C,0,75,2D,
PC 150  DATA C,B0,7,CD,21,3C,59,
FG 160  DATA 7,3C,6E,74,8,EB,E1,
EL 170  DATA 4C,CD,21,90,BA,80,1
GN 180  DATA 21,B8,C4,90,BD,0,0,
KC 190  DATA 3B,E0,75,F3,89,E8,9
NJ 200  DATA 43,68,6F,6F,73,65,2
FM 210  DATA 72,20,4E,20,28,6E,6
QJ 220  DATA 43,68,6F,6F,73,65,2
OK 230  DATA 70,74,69,6F,6E,20,2
KM 240 DATA /*

Program 2: FILES.BAT

echo off
rem Name: FILES.BAT
  [filename.ext]   See help
  panel for usage
break on
dir %1 >temp.dir
type files.mnu
  B"p    {CTRL-P}["i";"I"p
  [0;68;"X"p {CTRL-P}[2A
  }[0;68;0;68p {CTRL-P}[0m
if errorlevel 8 QUIT
if errorlevel 7 goto :a
if errorlevel 6 goto :h
if errorlevel 5 goto id
if errorlevel 4 goto :s
if errorlevel 3 goto :b
if errorlevel 2 goto :i
                goto :e
sort /+1 <temp.dir >con
goto :menu
copy files.hlp con
goto :menu
sort /+24 <temp.dir >con
goto :menu
sort /+14 /R <temp.dir >con
goto :menu
basic filegrp
echo -------- FILEGRP.BAT
  Created --------
goto :menu
dir %1 /p
goto :menu
sort /+10 <temp.dir )con
goto :menu

Program 3: FILES.MNU
{CTRL-P}[2J {CTRL-P}[32m
{SP 16}{218}{5 196}
{SP 16}{179}{SP 35}{179}
{SP 16}{179}{CTRL-P}[35m A
  {CTRL-P}[32m- Alphabetical
  order by filename {179}
{SP 16}{179}{SP 35}{179}
{SP 16}{179}{CTRL-P}[35m E
  {CTRL-P}[32m- Ext name
  order{SP 17}{179}
{SP 16}{179}{SP 35}{179}
{SP 16}{179}{CTRL-P}[35m D
  {CTRL-P}[32m- Date order, Yr
  not significant {179}
{SP 16}{179}{SP 35}{179}
{SP 16}{179}{CTRL-P}[35m S
  {CTRL-P}[32m- Size order{SP
{SP 16}{179}{SP 35}{179}
{SP 16}{179}{CTRL-P}[35m B
  {CTRL-P}{32m- Bat file
  creations FILEGRP.bat {179}
{SP 16}{179}{SP 35}{179}
{SP 16}{l79}{CTRL-P}[35m I
  {CTRL-P}[32m- Intrinsic
  order of dir entries {179}
{SP 16}{179}{SP 35}{179}
{SP 16}{179}{CTRL-P}[35mF1
  {CTRL-P}[32m- HELP{SP
(SP 16)C179)(SP 35)(179)
  -P}[32m- EXIT{SP 27}{179}
{SP 16}{179}{SP 35}{179}
{SP 16}{192}{36 196}{217}

Program 4: FILES.HLP

{SP 7}{201}{15 205}
{SP 7}{186}{SP 2}PURPOSE:
  Produces a directory
  listing{SP 17}{186}
{SP 7}{186}{SP 12}sorted in
  the desired order.{SP
{SP 7}{186}{SP 2}SYNTAX:{SP
{SP 7}{186}{SP 9}(if
  parameters are omitted, *.*
  used){SP 10}{186}
{SP 7}{186}{SP 56}{186}
{SP 7}{186}{SP 2}MENU
  OPTIONS:{SP 41}{186}
{SP 7}{186}{SP 4}A: Directory
  sorted ascending by
  filename{SP 11}{186}
{SP 7}{186}{SP 4}E: Directory
  sorted ascending by file
  extension{SP 5}{186}
{SP 7}{186}{SP 4}D: Directory
  sorted ascending by file
  date (mm-dd){SP 2}{186}
{SP 7}{196}{SP 7}giving
  calendar order, year least
  significant{SP 4}{186}
{SP 7}{186}{SP 4}S: Directory
  sorted DESCENDING by file
  size{SP 9}{186}
{SP 7}{186}{SP 7}allowing
  quick determination of
  largest files{SP 4}{186}
{SP 7}{186}{SP 4}B:
  FILEGRP.BAT created as : %1
  filename.ext %2 %3 %4{186}
{SP 7}{186}{SP 7}for editing
  and mass file copy, erase,
  type, etc.{186}
{SP 7}{186}{SP 4}I: Directory
  in the order of the
  directory entries{SP 2}{186}
{SP 7}{186}{SP 56}{196}
{SP 7}{186}{SP 4}H or F1:
  Displays this help panal{SP
{SP 7}{186}{SP 4}X or F10:
  Fast exit to DOS{SP 26}{186}
{SP 7}{200}{56

Program 5: FILEGRP.BAS
For instructions on entering this listing, please
refer to "COMPUTE!'s Guide to Typing In
Programs" published bimonthly in COMPUTE!.

NJ 10 'This program creates a ba
      tch file named FILEGRP.BAT
      , using the
LO 20 'TEMP.DIR file created by
      useful for
JF 30 'group file operations suc
      h as copying, deleting, pr
      inting, etc.
EK 40 'Each line in FILEGRP.BAT
      has the format: %1 filenam
      e.ext %2 %3 %4
GK 50 'Use a word processor or t
      ext editor to delete non-p
ID 60 'files from FILEGRP.BAT.
PE 70 OPEN "temp.dir" FOR INPUT
      AS #1'input file
MN 80 OPEN "filegrp.bat" FOR OUT
      PUT AS #2'output file
EC 90 FOR X= 1 TO 4:IF EOF(1) TH
      EN SYSTEM'skip 4-line head
       ck for end-of-file
LA 120 LINE INPUT #1,X$'get inpu
       t line
DI 130 IF LEFT$(X$,l)=" " GOTO 1
       10'skip lines beginning w
       ith space
GE 140 Z=INSTR(X$," "):Z=Z-1'fin
       d length of filename
EG 150 PRINT #2,"%l ";MID$(X$,1,
       Z);".";MID$(X$,10,3);" %2
        %3 %4"'form output
HL 160 GOTO 110'continue till en