Today is an exciting day in the CLI and Batching series! We are going to start working with batch files. If you have been following the series, you should know how to navigate in CLI, how to create text files, move them and delete them, know what environment variables are, and know how to use the echo and set commands.
Before we begin, you might want to create a folder to store your batch files. In previous posts, I suggested using a folder on your desktop named ‘batch files’. However, you can name it and store it where ever you like. However, in future posts, I will be assuming the suggestion I made for examples.
If you have not already, you might want to read the Series start: CLI and Batching post first. The reason why is because I addressed a couple of issues about working with batch files (such as how to create one.)
Ironically, we will be working in the GUI. The GUI is nice for multitasking. When even creating one batch file, I usually work with three windows: A command line window so that I can test out commands and access help, notepad for editing the batch file, and the batch file itself while executing. The nice thing with GUI is that you can edit the lines in the batch file through notepad, save it while leaving notepad open, and run the batch file with the new changes. You do not have to have a command window open if you don’t want to (start->run, cmd), but It is a lot easier and faster when you run into issues.
Our first batch file is going to be a simple calculator. Even though there is one in your accessories in GUI and our batch file will not be good enough to replace that, creating a calculator is fantastic for learning purposes. Keep this batch file saved, because we will be building on it in later posts.
Open up your batch files folder, right-click to create a new text document, and rename it to calculator.bat. Accept the changes. If it does not change the type of the file from a text document to a batch file, you are probably not seeing extensions for known file types. Please read the Series start: CLI and Batching post to solve this. Now, right click the file, and select ‘edit’. This will bring up notepad.
Looking at the blank file, where do we start? Is your mind as blank as notepad? Now is a good time to think of the flow of your batch file. Most programmers write code based on a flow chart called pseudo code. Pseudo code, in programming, is used to solve the logic of a programming task through flow charts or steps. This is for your benefit only. Therefore, while there are official charts and symbols you can use, there really is no right or wrong way to do pseudo code. This code is for flow process only. It is used to ask the following questions:
- What is the purpose of the program you are trying to create?
- What are the steps that are needed to be accomplished?
- What choices does your user(s) have?
- What routes can your program take based on your user’s choices?
If I were to explain the proper way to do pseudo code, this would be a long, long post. However, it is great to learn because not only it would be easier to share with other programmers, there is specialized software you can get where you can use pseudo code to create an actual program while the software will convert your pseudo code into programming code. After you create pseudo code, it is much easier to write your programming code.
For this post, our batch file will do addition only. We will build the batch file to use other arithmetic in later posts. Since the batch file is simple, the pseudo code is simple. This is something we can do in our head, but I will spell it out here in these steps:
- Prompt user to enter the first number
- Get the first number from the user
- Prompt user to enter the second number
- Get the second number from the user
- Add the first number and second number
- Save the sum in a variable
- Show the user the sum of the two numbers
Sounds easy enough, doesn’t it? Using these simple steps, we now know where we need to go in our batch file. Other than getting user input, you should know how to do the other steps from the previous posts. Don’t worry, I won’t leave you in the dark. I will tell you how to get user input when we come to it.
First things first. Before we enter any code or commands, we should write what our batch file does for future reference. These are notes that have no significance in running the batch file; just references when we edit it. In batch files, these are called remarks. Remarks are used in batch files to help the programmer understand the code. The REM command is used to enter remarks in a batch file. It tells Windows not to execute the command, but save the line in the file so that it can be read while editing. So, your first two lines should be something like this:
Line 1: REM Copyrighted 2011 by Richard Hughes (replace by your name and current year, I am not really copyrighting this, just giving you an example
)
Line 2: REM This batch file is used to add two numbers together
You don’t want too many remarks, or else your code will look more complicated than it is. You also want to use enough remarks so that you can understand what you are trying to do with code you are using.
NOTE: In most other platforms and programming languages, ‘remarks’ are called ‘comments’. Usually, they are marked with a special character. For example, remarks can be marked when a line starts with a pound (#) sign. However, no matter the platform or the syntax, remarks and comments are used for the same thing.
Alright, based on our pseudo code list, we want to first prompt the user to enter the first number. This is done with the echo command. So:
Line 3: echo Enter the first number:
Now, we want to read input from user. Thankfully, we can use the set command to do this. Using the /p flag, the set command will read input and put it into a specified variable. So:
Line 4: set /p num1=
This will read input from the user and set the num1 variable to that value.
For steps 3 and 4, we are basically going to repeat the process to get the second number. So:
Line 5: echo Enter second number:
Line 6: set /p num2=
Now that we have both numbers, the next steps are to add them together and display it to the user. You should know how to add two variables together and make a third variable from the sum based on the previous post.
Line 7: set /a num3=%num1%+%num2%
Line 8: The sum of %num1% and %num2% is: %num3%
We are just about done. However, after the last line is executed, the batch will close, so you will not be able to see the result. Adding the pause command will solve this by pausing the batch file until a key is press.
Line 9: pause
Your batch file should look as followed:
- REM Copyrighted 2011 by Richard Hughes
- REM This batch file is used to add two numbers together
- echo Enter the first number:
- set /p num1=
- echo Enter the second number:
- set /p num2=
- set /a num3=%num1%+%num2%
- echo The sum of %num1% and %num2% is: %num3%
- pause
NOTE: I am using the line numbers as references, you don’t put them in the batch file.
After you entered these lines in notepad, save it, and double click the batch file to run it. Did it work? Good! However, it will display every command before executing them as a default. This is good for debugging. But, if your batch file is working correctly, you can turn this off by the ‘echo off’ command. Echo off will still be displayed first before executing, so you can clear the screen afterwards so the user cannot see that command as well via the cls command. The cls command clears the screen. Update your batch file to look like this:
- REM Copyrighted 2011 by Richard Hughes
- REM This batch file is used to add two numbers together
- echo off
- cls
- echo Enter the first number:
- set /p num1=
- echo Enter the second number:
- set /p num2=
- set /a num3=%num1%+%num2%
- echo The sum of %num1% and %num2% is: %num3%
- pause
After you have made the changes, save the file and rerun it. It should now work the same, look a little more clean. Save this file. We will use this in future posts.
See how useful pseudo code can be? You might want to write it out first before writing your batch file as a habit. After you become more comfortable with pseudo code, it is something you can do in your head for the more simple tasks. Pseudo code helps you write cleaner code and makes it easier for you to do tasks you have not previously done. You just solve the logic with pseudo code and then Google for the correct command and syntax when coding.
Join the forum discussion on this post

