Lesson: Put a Menu in Your BAT File

[home] Anguilla Library Computer Club
Home | Lessons | Anguilla | News | Email
Last time you created a boot disk with programs on it. Now improve autoexec.bat to ask which program to run. Use the same floppy disk as before and get to the DOS prompt. When you "boot" your PC it performs the commands in autoexec.bat. You can make a BAT file very smart using the GOTO, CHOICE and IF commands, plus labels to jump to with GOTO.
a:\>cd \
a:\>edit autoexec.bat
echo off
prompt $p$g
choice Play Adventure?
if errorlevel 2 goto googol
cd a:\adv
a:\>help choice
Edit a:\autoexec.bat to add choice logic. Any line starting with a colon is a label that you can GOTO, as in :googol. The CHOICE command prints a prompt message and waits for the user to type Y or N. If the answer is Y, errorlevel is set to 1. If N, then 2. The IF command checks errorlevel for 2 (NO) and uses GOTO to jump around the run of Adventure. For more information on these commands you can use the HELP command. To test your BAT file, just type autoexec. To stop a misbehaving BAT file, press CTL-C.
a:\>cd \
a:\>edit autoexec.bat
choice Googol Math?
if errorlevel 2 goto tme
cd \googol
choice Typing Made Easy?
if errorlevel 2 goto aseasy
cd \tme
choice Spreadsheet?
if errorlevel 2 goto end
cd \aseasy
cd \
choice DOS?
if errorlevel 2 goto start


You now have the tools you need to add options to autoexec.bat for all your programs. Add a CHOICE question for each program and skip around the CD and execute commands if the user answers N. After each edit, exit back to DOS and test autoexec.

After the last program choice, ask the user if he wants to go to the DOS prompt or not? If the answer is N, jump to the :start label of autoexec.bat; if the answer is Y, the BAT file will continue to the end of file and stop. The user will then see the DOS prompt.

Test your new BAT file by typing autoexec and by pressing ALT-CTL-DEL. Be sure you can run each program. Congratulations--you can use this portable software menu on any PC with a matching floppy drive. Put your boot disk in a Ziploc bag to protect it. Take it home, impress your friends.

Extra Credit: Using the ECHO command and options of the CHOICE command, you can make this boot disk even more convenient by asking the user which program he wants to use.