Sometimes in building programs for the purpose of user-interface, it is necessary to reach deeper for a richer UI experience. That’s where the menu comes in. Menus have long been used to help programmers organize information, and users locate it more efficiently. In this lesson we’ll go over how you can build and integrate a menu for the Windows shell environment using batch process programs and how to execute commands from them .
To start, of course we’ll need to utilize the handy ECHO command. This command, of course, displays information publicly to a user, limiting their access to the command-line. This can be most helpful, especially in designing professional applications. Today we’ll be using a hierarchical menu type that focuses on parent information, and then extends to details (the way that most menus are developed). Take a look at the example below.
@ECHO OFF
ECHO HELLO THERE! PLEASE SELECT A CHOICE FROM THE MENU BELOW BY TYPING THE CORRESPONDING NUMBER AND PRESSING ENTER.
ECHO 1.) THIS IS CHOICE 1
ECHO 2.) THIS IS CHOICE 2
set /p var=
pause
Okay, notice that the menu prompted the user with instructions before any choices had been presented using the ECHO command. Also notice that the first time that any user input module was introduced was the invisible SET /P VAR= command line. This command line would generally have prompted a user with a message, but we’ve chosen to make it invisible, because placing it above the menu would force a user input a choice before any menu choices had been presented. So, what we’ve done is correlated the users choice to the variable VAR. After all is said and done the menu would have been presented to a user like this:
HELLO THERE! PLEASE SELECT A CHOICE FROM THE MENU BELOW BY TYPING THE CORRESPONDING NUMBER AND PRESSING ENTER.
1.) THIS IS CHOICE 1
2.) THIS IS CHOICE 2
|
We’ll allow this template to become the form of what will soon be a functional menu, but replace some of the menu choices. Let’s assume that you’re building a program that will present choices about what Internet browser you would like to start. We’ll need to incorporate some richer commands to do so. Take a look at the example below, and we’ll go over it to follow:
@ECHO OFF
GOTO START
:ERROR
CLS
ECHO I’m sorry! That is not a valid choice!
:START
ECHO PLEASE TYPE THE NUMBER THAT CORRESPONDS WITH THE PROGRAM THAT WOULD YOU LIKE TO RUN, THEN PRESS ENTER.
ECHO 1.) FIREFOX
ECHO 2.) INTERNET EXPLORER
ECHO 3.) GOOGLE CHROME
set /p var=
IF %var%==1 GOTO FIREFOX
IF %var%==2 GOTO IEXPLORE
IF %var%==3 GOTO CHROME
GOTO ERROR
:FIREFOX
START FIREFOX.EXE
GOTO END
:IEXPLORE
START IEXPLORE
GOTO END
:CHROME
START CHROME.EXE
GOTO END
:END
EXIT
Notice that the first command of the process is GOTO START. This command ensures that the program will not read the error prior to any user input. In this case, I’ve decided to place the error message above the user input to eliminate any bugging and present a cleaner script. This way, if the user was unable to commit to a valid choice the GOTO ERROR command will just take him or her to the error message, and begin again. Otherwise, we might have had to program several IF NOT command lines, and that can get messy. Also notice that we’ve programmed individual modules such as :FIREFOX , :IEXPLORE, and :CHROME. Again this is not necessary, but in the instance that our menu might have been multi-tiered, this just saves us time and appears more organized. And since another program or utility is unable to call on a specific line of code, the module will be able to take it’s place. In other words, we can program another process to utilize the :FIREFOX module, where we WOULD NOT be able to program the same process to locate START FIREFOX.EXE in a program. The use of the rapid app development should be kept in the mind of the person. The starting of the program of the business will be as per the specifications of the business person.
Keep in mind that you can attach menus to menus (the hierarchical structure that we were referring to earlier) by creating modules (the same way that we did in the example above. For example:
@ECHO OFF
GOTO START
:ERROR
CLS
ECHO I’m sorry! That is not a valid choice!
:START
ECHO PLEASE TYPE NUMBER CORRESPONDING TO THE THE CLASS OF FOOD THAT YOU PREFER MOST.
ECHO 1.) MEAT
ECHO 2.) VEGETABLES
ECHO 3.) DAIRY
ECHO 4.) FRUIT
ECHO 5.) WHEAT, RICE, ETC.
set /p var=
IF %var%==1 GOTO MEAT
IF %var%==2 GOTO VEG
IF %var%==3 GOTO DAIRY
IF %var%==4 FRUIT
IF %var%==5 GLUTEN
GOTO ERROR
:MEAT
GOTO MEATSTART
:MEATERROR
CLS
ECHO I’m sorry! That is not a valid choice!
:MEATSTART
ECHO PLEASE TYPE NUMBER CORRESPONDING TO THE THE TYPE OF MEAT THAT YOU PREFER MOST. TO GO BACK, SIMPLY TYPE BACK.
ECHO 1.) CHICKEN
ECHO 2.) BEEF
ECHO 3.) PORK
set /p MEAT=
IF %meat%==1 ECHO I LOVE CHICKEN AS WELL!
IF %meat%==2 ECHO I REALLY LIKE BEEF!
IF %meat%==3 ECHO EH, PORK ISN’T MY FAVORITE
IF %meat%==BACK GOTO START
GOTO END
pause
Note that the other modules were not completed (so the meat module will be the only working one). In this example, we’ve set up our menu in the same way as before, but now we rely on child menus. Remember (from previous articles), that a child menu is a menu that is essentially only reachable through a parent, or fundament. This reliance helps us create a hierarchical menu structure which will in turn give us exactly the platform that we will need to create more advanced structures, and so on.
Make sure to experiment with this material, seeing as it extremely useful in the Window’s shell environment due to the lack of graphical capabilities. We will spend more time adding form to the function of these powerful utilities in the future, and learning to incorporate other programs and languages into the process as well, but until then experiment, have fun and happy programming!
Thanks for reading, and don’t forget to continue on to learn more about command-line and batch process programming by referring to The Principles of Batch Process Program Development series.