|Everything HP200LX: Knowledge, Products, Service|
Create an Interactive Batch File Using ECHO
Ed shows how to use the MS-DOS ECHO command to turn a batch file into a program that will accept input.
A batch file is a keystroke saver an unformatted file that lets you run a series of DOS commands. For example, open a blank Memo screen and type in these three lines: d: cd\DOS dir Save this batch file as C:\LIST.BAT and exit to the DOS C:\ prompt by pressing (&...) Dos. Type list and press (ENTER) to run the batch file. The first line of the batch file changes to the D drive, the second line changes to the DOS directory, and the third line executes the DOS dir command, listing the files in that directory.
A normal batch file like the one described above runs a series of DOS commands. Once you start the batch file, it executes the commands one after the other, without accepting any further input from you. For most users, the ability to run a series of DOS commands is all they want out of batch files. However, some users want to be able to do more with batch files. Specifically, they want to make batch files interactive. They want batch files to be able to ask questions of a user, accept his or her answers, and respond accordingly.
Here's a simple example batch file that shows how a batch file can be used to simulate a Yes/No interaction. Open Memo, type in the lines exactly as shown below and save the Memo file as C:\ YESNO.BAT. (All batch, script and .COM files referenced in this article are archived in ECHO.ZIP <See Shareware/Freeware index>.)
Once you've saved the file as C:\YESNO.BAT, exit to the DOS prompt, type yesno and press (ENTER) to run the program.
This batch file gives you the option of choosing Yes or No. If you choose Yes, a message is displayed telling you that you should chose Yes. If you choose No, you get the opposite message.
The awkwardness of the technique shown in the above example, using the COPY CON command, inspired others to create small, executable files to accomplish the same thing. The batch file would run the executable file. The executable file accepts your input and then runs the rest of the batch file. Eventually, Microsoft included its own input program, CHOICE .COM, in version 6.0 of MS-DOS.
YESNO2.BAT, listed below, is a version of the YesNo batch file, using the MS-DOS CHOICE command.
This batch file is simpler, but you'll have to have a copy of CHOICE .COM, found in the C:\DOS directory of a PC running DOS 6.0 or greater.
Unfortunately, CHOICE.COM is not found on the HP Palmtop. But read on and you'll see how to create an interactive batch file without using CHOICE.COM.
Creating a stand-alone, interactive batch file using the ECHO command to embed programming code into a batch file
The typical use for the ECHO command is to display a message on the screen. You can also redirect the message to a file. For example, go to the DOS prompt, type echo George and press (ENTER). The word George is displayed on the screen. For another example showing the redirect capability, go to the DOS prompt, type echo George > line.txt and press (ENTER). This command redirects George to a file called LINE.TXT (The > symbol causes the LINE.TXT file to be created.)
A more unusual use for the ECHO command is to have it redirect machine-readable characters (binary computer code) to a .COM file. This technique lets you use the ECHO command to create executable files. When used in a batch file, the batch file can create .COM files on the fly. This can be useful, as you will see.
The process involves: 1. Using the built-in Debug program to assemble a small .COM program; 2. Embedding the binary code from the .COM program into an ECHO statement in a batch file; and 3. Removing the assembler code and the .COM file from your disk.
CREATING THE .COM PROGRAM
The process of creating .COM programs involves the creation of script files and turning the script files into executable .COM files using the built-in Debug program. Script files are nothing more than text files containing lines of computer code. You use the built-in Memo program to create the script file (or any word processor capable of saving a file in plain ASCII format).
The process of using Debug to assemble the script files into .COM programs is described in the sidebar on this page. As mentioned earlier, these programs, as well as the .BAT files described in this issue, are available on the Jan/Feb 96 issue of The Palmtop Paper On Disk, archived in ECHO.ZIP <See Shareware/Freeware index>
Example 1: A simple "Yes/No" program
CREATING A .COM PROGRAM
One of the simplest but most important thing an interactive batch file must do is be able to ask a Yes, No question. Save the Debug script file shown below as YN.SCR and use Debug to turn YN.SCR into the executable program YN.COM as described in the sidebar.
N YN.COM ; Give output a file name
When assembled the YN.COM program will be 22 bytes long.
When YN.COM is run, it waits for the next single keystroke to be entered (Y or N, and no others) and stores the ASCII keycode value in the DOS errorlevel variable buffer. (This is simply a memory location reserved by DOS to store a certain type of information.) This makes it possible to set up a batch file that runs YN.COM, stores the ASCII keycode entered by a user, uses if statements to test for a specific errorlevel variable, and executes different commands based on the input.
The TESTYN.BAT batch file shown below shows how this .COM program works and lets you test the YN.COM program. See the sidebar on page 42 for instructions on creating batch files.
But before running this batch file, please back up everything on your disk. If you're working on the HP 100/200LX, be sure to close all applications.
The batch file begins by turning ECHO off so commands in the batch file are not displayed on the screen as they are executed. Then it displays the question Do you want to quit? [y/n] and calls the YN.COM program. YN.COM stores the ASCII code for the next keystroke you type in, in the DOS errorlevel variable buffer (49 for N, 21 for Y). YN.COM is designed to only accept a Y or N. It will sit there doing nothing until you enter one of those two characters. Errorlevel is really a misnomer, since we're not expecting any errors. Well use the errorlevel memory buffer as a way to store the input from YN.COM and pass it on to TESTYN.BAT. In this case, the errorlevel number can only be 49 or 21 depending on whether you pressed the N or the Y key.
The batch file then tests the errorlevel values with a couple of if errorlevel statements. The first, if errorlevel 49 goto again checks to see if you answered N for no. If you did, the if command sends the batch file back to :again (line 2) and you start over again with the Do you want to quit? message. If you enter Y for yes, you skip by the first if errorlevel line and if errorlevel 21 goto end sends you to :end. From there, the message Quitting the testyn program is displayed and the batch file ends.
Since the if errorlevel statement checks for values that are greater than or equal to a given value, the TESTYN.BAT file is designed to test for the higher value before testing for the lower value.
EMBEDDING YN.COM BINARY CODE IN THE BATCH FILE
We are doing this so we can create a stand-alone batch file that doesn't depend on another program file to work. Once you're satisfied that YN.COM behaves properly, its time to embed its binary code in the TESTYN.BAT file. To do this, you'll need to use a text editor that displays binary data. Memo, on the HP Palmtops, does not display binary data, so you'll have to use a text editor like QEDIT <See Shareware/Freeware index>. The instructions below assume you are using QEDIT.
Load the TESTYN.BAT into QEDIT, go to the 4th line that contains YN and insert a blank line above and below it. On the line above YN, type echo (put a space after ECHO.) Use the editor's block read command to load the YN.COM file onto the same line as ECHO . Then go to the end of the line and append the characters > yn.com .
On the line below YN, add the characters del yn.com and save the file.
When you run the TESTYN .BAT program, the super ECHO statement you just created will use I/O redirection to recreate the YN.COM file. The YN command will run it and the next command will delete it.
Pat yourself on the back. You've just created a simple stand-alone interactive batch file that contains binary code. It creates its own executable .COM file, runs it, and then cleans up after itself by deleting it. Call it a stupid DOS trick, but it turns out to be very useful, as you'll see below.
Example 2: Create a simple menu system for your Palmtop
Here's a way to use the trick we just learned to create a simple batch file menu system. Key the following script file into a text editor and save it with the file name ETCESC.SCR (don't forget the blank line at the very end).
Then use the command: debug < etcesc.scr to generate the ETCESC .COM program.
This executable .COM file accepts a keystroke for one of the characters: E, T, C or ESC, and then stores the ASCII value of that key in the errorlevel variable buffer.
The MENU.BAT file, shown below lets you check out the performance of ETCESC.COM, and begins to set up the menu system.
The first thing the batch file does is to turn ECHO off and display the message: Programming Options : [E]dit [C]ompile [T]est [ESC]. Depending on which key is pressed (E,T,C, or ESC), the batch file will display one of the following messages:
Pretending to run compiler
Pretending to test the program
Pretending to edit the file
Pretending to quit the batch file
We haven't yet set the batch file up to run any programs, so were just pretending at this point.
When you're convinced that the batch file and ETCESC.COM file are working properly, load the MENU.BAT file into QEDIT and insert the word echo (make sure its followed by a blank space) on a new line before line 3 (the one containing ETCesc). Append the ETCESC.COM file to that ECHO command as described in the previous example. Then add >etcesc .com to the end of the line. Put the command del etcesc.com on a blank line after ETCESC and save the batch file. MENU.BAT is now a stand-alone interactive file. Let's turn it into a real menuing system.
Example 3: "Stuffing" the keyboard
Suppose you wanted to set up the MENU.BAT file described above so that one of the programs in the menu was Borland's C Compiler (BCC). One of the simplest things to do would be to enter a command line in MENU.BAT containing the program name followed by the file you wanted to compile (for the C Compiler example you would replace echo Pretending to run compiler with bcc filename .cpp). Unfortunately, this would cause the batch file to remain in memory while the Borland C Compiler program is running. The batch program won't quit until it can jump to the end of its code.
The third script file, PUTCR .SCR, can be used to create the PUTCR.COM program. Key in the script file below and create PUTCR.COM using Debug as described earlier.
N PUTCR.COM ; Name the output file.
This program takes the character or command string on the command line and stuffs it into the keyboard buffer along with the code for the ENTER (carriage return) key. If you use PUTCR .COM in a batch file, you can execute a DOS command after the batch file ends.
This strategy can help us launch a program from the menu system after the batch file has ended. Replace echo Pretending to run compiler with putcr bcc filename.cpp (instead of bcc filename.cpp). This stuffs bcc filename.cpp into the keyboard buffer, the batch file then jumps to the end of its code. As soon as the batch file ends, the characters in the keystroke buffer are executed. The batch file will be out of the way and won't take up any memory.
Traps and workarounds
"OFF-LIMIT CHARACTERS" Using the ECHO statement to create a .COM file has its limitations. It works fine as long as the string of binary code does not contain any of the characters:
Nul Ascii 00 hex
Tab Ascii 09 hex
NewLine Ascii 0A hex
Enter Ascii 0D hex
> Ascii 3C hex
< Ascii 3E hex
| Ascii 7C hex
The ECHO command will interpret these codes rather than display them.
For example, if ECHO encounters < , it will try to read from a file rather than display <. And when it fails, it will respond with File not found.
Likewise, the Nul byte (ASCII 0) will be interpreted as a space (ASCII 32) and the Tab character may be interpreted as 8 spaces, depending on how your editor deals with the Tab character.
One way to check for the presence of these off-limit characters in a .COM file is to load the COM file into Debug and use the U command to unassemble the program. The listing will look something like this.
The column to the left of the assembler instructions contains opcodes for the instructions. The opcodes will reveal if Debug generated any of the off-limit codes: 00, 09, 0A, 0D, 3C, 3E, or 7C. If you find any of these opcodes, you'll need to rewrite the instruction to eliminate them.
For example, in the above listing, the first line could have been MOV AH,0. However that would have produced an opcode of 00. Using the equivalent instruction, XOR AH,AH, produces a different pair of opcodes. They have the same effect, namely setting the AH variable to zero.
The PUTCR.SCR file shown on page 44 has some other work-arounds. This file uses three commands XOR CH,CH, MOV CL,0C, INC CL, to do the same thing as the single command MOV CX,000D. This bit twiddling is needed, if you want to use the binary code in the ECHO command. To put this another way: if you don't eliminate these opcodes from the final COM file, you risk hanging the computer or, getting error messages that say File not found, etc.
Example 4: A DOS READLN Command
One of the commands found in many programming languages, but missing from the DOS batch language, is READLN (read a line). This command reads input from the keyboard and appends a new line character at the end of the input. With a slight modification, READLN can read its input from a disk file rather than from the keyboard. For example, in the Pascal programming language, the command READLN (name) activates the keyboard and lets you type in a string of characters. When you press (ENTER), the string of characters is stored in a variable called name. The MS-DOS batch language doesn't have any such command.
Most often, the COPY CON filename command is used as a work-around. The only problem I have with COPY CON is that I always forget to press (CTRL)-(Z) to end the input.
The script file shown immediately below will generate READLN .COM. This program adds READLN capability. Like the other script files in this package, READLN.SCR has been optimized to eliminate any off-limit opcodes.
N READLN.COM ; Name the output file
This version of READLN.COM is mostly the work of Dave Vickers, who wrote the file input/output parts of the code. It will work either at the DOS prompt or when run from FILER.
The TESTRDLN.BAT file shown below shows how to use the READLN.COM program.
After shutting off ECHO, the batch file displays the following prompt: Enter commands. Use Enter alone to quit. Then the batch file calls the READLN program. The READLN command must contain the name of a batch file on its command line. Failure to name an output file will cause READLN to send its output to the COM port, or to some other place in your computer, and may lock up your system and require you to reboot your machine. When READLN runs, it puts a > prompt on the screen. From this prompt you may enter any DOS command, such as DIR /W, and press (ENTER). Another > prompt will appear. Key in pause and press (ENTER). At the next > prompt type CHKDSK c: and press (ENTER). Finally, at the > prompt, just press (ENTER) and the three commands will be executed via the CALL $ command. The $.BAT file will be deleted and the TESTRDLN .BAT file will end.
A second batch file, TESTSET.BAT, shows how you might use READLN .COM, along with the ECHONL.COM program, from a previous issue of The HP Palmtop Paper, to get a user's name and store it in a DOS environment variable for use by the TESTSET program.
As before, you can use your text editor to embed the binary code for READLN.COM in an ECHO statement and redirect the output to a file, $READLN.COM. Use the command $READLN $.BAT and you can delete both the COM and BAT files with a single command, DEL $*.*.
Hope this gives you some food for thought. Until next time: Happy Programming!
Copyright © 2010 Thaddeus Computing Inc