In our last post, we have created a simple simple calculator that does addition only. While this might be impressive if this is your first batch file, this program doesn’t have the ability to be sold at a dollar store. What about subtraction, division and multiplication? Surely we can create four different batch files, but how would that be efficient to a user?
Well, I am going to have you create four different batch files. Then, we will tie the four batch files with a fifth one so that it will appear like one batch file. If you have not already done so, follow through the last post Creating a Batch File Using Pseudo Code and create the calculator.bat file before continuing.
Open up your batch files folder. Now, right-click the calculator.bat file, choose copy, then right-click anywhere else on the folder, and choose paste three times. Rename the calculator.bat file to add.bat. Then rename the other files to subtract.bat, divide.bat and multiply.bat.
Now you need to change subtract.bat, divide.bat and multiply.bat so that it does as the name applies. Right-click subtract.bat and choose edit. Change the line that says ‘set /a num3=%num1%+%num2%’ to ‘set /a num3=%num1%-%num2%’. In the next line, change the word ‘sum’ to ‘difference’. Save the file, and run it to make sure the changes took effect like it should.
Now, change the divide.bat and multiply.bat files the same way (based on their name of course!).
We are now going to create our menu batch file. Right-click anywhere in the folder and create a new text document. Rename it to ‘calcmenu.bat’. Right-click the file and select ‘edit’.
The logic behind a menu might be simpler than you think. We can do a menu by following these simple steps:
- Display the menu to the user
- Have the user enter an option
- Check the option to make sure it is valid
- Handle the option if not valid
- Do the selected option
In this example, we are creating a menu so that the user can open the appropriate batch file. However, menus can be used to do things inside the current batch file as well. In fact, we could have combined all 5 batch files into one if we wanted to…but, it is convenient this way so that I can explain a few new concepts
.
First, let’s display the menu. A basic menu can look like this:
- echo off
- cls
- echo ________________________________
- echo Main Menu
- echo —————————————————
- echo 1. Add
- echo 2. Subtract
- echo 3. Multiply
- echo 4. Divide
- echo 5. Exit
- echo Please select an option
Now, we want to have the user enter an option and save it in a variable:
- set /p ans=
For now, I will show you how to make this work. Then, we will handle wrong choices (anything that is not a 1-5 answer.)
After the user enters a choice, we want that user to go to that part of our program, being somewhere else in the batch file or another batch file. Conveniently, there is an actual goto command. The goto command is used to skip code until the specified marker is reached, then executing code under the marker. A marker is used to mark a line in a batch file. Markers are marked with a colon (:). When Windows sees that, it knows that it is a marker and not to execute anything on that line.
When the user enters an input for our menu, the user is actually specifying what marker to use. So, our next lines should be:
- :1
- :2
- :3
- :4
- :5
Great! But, our markers are currently not doing anything. We want these choices to run the correct batch file (or exit) depending on what the user has selected. Thankfully, this is very easy. You can call batch files the same way you can call commands; all you have to do is type in their names. Not only that, you can call their names via the absolute path or the relative path.
Whenever you open a batch file, the working directory that it uses is the folder that it is located at. So, since all the batch files are in the same folder, their relative paths are the same, so you don’t have to worry about their paths as a programmer at all!
Finally, there is one more command worth mentioning: the exit command. The exit command simply ends a batch file from running. With all this in mind, the menu file can work simply by doing this:
- :1
- add.bat
- :2
- subtract.bat
- :3
- multiply.bat
- :4
- divide.bat
- :5
- exit
Go ahead and save calcmenu.bat and run it. Make sure that the correct batch file opens. As you run it, you might notice two things you might want to add in the other batch files:
- Show the function of what you are doing at the top of the screen. That way, you know for sure what option you have selected (addition, subtraction, division or multiplicaton).
- At the very last line of all of them, you might want to add calcmenu.bat so that you end up back at the menu after the batch file is done.
Make sure everything is working. When it is, there is just one more thing we have to add to calcmenu.bat: error handling.
Thankfully, since we are using the goto command and numbers, this is quite easy. We can use a sixth marker for everything else. We can then prompt the user to select a valid choice, and then rerun the calcmenu.bat file. Enter this after everything else in calcmenu.bat:
- :6
- echo you must choose a valid option (1-5)
- pause
- calcmenu.bat
Now that we have our ‘error’ marker, we need to specify when to go there. It will work if the user enters ’6′, but for everything else you would get an error ‘marker not found’, which will then close the window so fast you cannot read the error.
In order to specify when to run this, we need to specify a condition. A condition is a statement that is evaluated if something is true or not. For our example, the ans variable needs to be between 0-6. If it is not, then we need to go to marker ’6′.
A great command that uses conditions is the if command. The if command evaluates a condition, and then executes code if the condition is true. Optionally, the else command can be used as well. The else command is used to perform a task if a condition is not true. A correct statement of this would be something like:
IF ‘the traffic light is red’, THEN press your breaks. ELSE, press your accelerator.
NOTE: EVERY script or program language uses if statements. However, the syntax is usually different. Some require the ‘then’ command. Some require brackets. However, if is very powerful. I have written complex programs based solely on if statements.
What we want to do is use two if statements. One statement evaluates if the ans variable is not bigger than 0. The second if statement evaluates if the ans variable is not smaller than 6. This is very easy to do with the not command. The not command is used to inverse a condition. If a condition is false, the not command makes it true, and vice versa.
I know that it is a lot to take in. I will devote an entire post on if statements and conditions. But for now, put this right under the ‘set /p ans=’ line and before the ‘goto %ans%’ line:
- if not %ans% gtr 0 goto 6
- if not %ans% lss 6 goto 6
NOTE: gtr means ‘greater than’ and lss means ‘less than’. In windows, three characters are used to represent number conditions. In some platforms, the actual signs are used (like ‘<’ for less than.)
Save and run calcmenu.bat again. See what happens if you choose a value not from 1-5. Also, make sure the other choices are working.
I have included my version of our calculator program here. After you have downloaded the zip file, you might want to extract it. Then, click on ‘calcmenu.bat’ to start it.
Join the forum discussion on this post
