Top 3 Products & Services


Dated: Aug. 12, 2004

Related Categories


For affordable web design, web hosting, domain registration, and custom programming services, visit .

After a break from weekly articles over the Christmas/New Year period, we return today with our second batch tutorial.

Today you're going to learn how to make a simple "operating system" using batch files. You'll learn about the following commands and features: echo, cls, choice, if, errorlevels, and labels. Once you've finished, you will have a batch file that can show a few different menus, enter into the menus and display the options contain within, and let the user pick the option they want. The example we are going to use is going to let the user choose which game they want to play.

Note: If you are running Windows XP, some of the commands shown in this tutorial will not work. You will need to work through this tutorial on a computer running Windows 98 or plain DOS.

To get started, open up Notepad (if you are running in Windows). If you are running in DOS, open up the MS-DOS Editor (just type edit at the command prompt and press enter). Before you begin, save your file and call it "gamesmenu.bat". If you are using Notepad, make sure you select the "All Files" option, otherwise your program will end up being saved as "gamesmenu.bat.txt", and it won't open correctly.

The echo Command

The echo command can be used for two different objectives: to hide all the commands you use, so the user doesn't see them, and to show a custom message to the user. In this example, we're going to use it for both. To get started, type the following as your first line:

echo off

This simple command tells the computer that, for the rest of your program, the commands you use will not be shown. To get an idea for what this does, when you've finished come back and take out this line and notice the difference it makes.

Now, the echo off command does tell the computer not to show the commands, but it won't be in effect for the echo off command itself. And, we don't want the user to see that, so we'll need to clear the screen. The next command does just that:


Enter that command into your program now. "cls" stands for "Clear Screen".

The other use of the echo command is to give the user a custom message. Try this:

echo Welcome to my Game Chooser operating system.

Notice that you don't need any quotes around the message, as you do in most programming languages.

Now would be a good time to test out your program. Save it again, and then open it up. If you are using Windows, all you need to do is locate the file and double-click on it. If it opens and closes straight away, you may want to open up an MS-DOS Prompt. Look for it in your Start Menu under Accessories. Another alternative is to add the pause command on a new line at the end of your program. You'll see what it does when you run it.

Second Batch TutorialIf you are using DOS, you'll need to close down the editor, then just type the name of your file ("gamesmenu") at the command prompt, and press enter. Notice that you don't need to enter the ".bat" - DOS will figure that out for itself.

You would have noticed that the pause command just adds in a prompt that reads "Press any key to continue". It does come in handy!

Now, try taking out the echo off and see the difference it makes. Now you realise why we want to keep it in there :.

choice, if, errorlevels and labels

The choice command gives the user of your program an opportunity to choose what the program is going to do next. The if command tells the program what to do if the user selects a certain option. The errorlevel command is needed to help the program decide. Finally, labels mark a section in your program that you can "jump" to at anytime. In this example, we are "jumping" to different sections depending on what the user chooses.

Now, take out the line that has the pause command on it, then type in all of the following code, and then we'll go through it.

echo Press the corresponding number to play the game of your choice.
echo 1. Duke Nukem 3D
echo 2. Commander Keen 4
echo 3. Epic Pinball
echo X. Exit this program
choice /c:123x
if errorlevel 4 goto exit
if errorlevel 3 goto pinball
if errorlevel 2 goto keen
if errorlevel 1 goto duke3d

echo Duke Nukem 3D will now run.
goto start

echo Commander Keen 4 will now run.
goto start

echo Epic Pinball will now run.
goto start


And one more thing - add the following line of code to the very top of your program. We'll get to the meaning of this soon.


Run your program now and see what it does. When you're given the choice of games, try pressing a number (1, 2 or 3). Try all of them, and then press X to exit. Can you get an idea of how it works? Let's go through it.

You already know what the echo commands do, so next is the choice command. You'll notice that using the echo command, we gave the user some choices of games. This isn't required for choice to work, but we do it because the user probably would like to know what they're choosing :. In other words, you don't need the information before a choice command to make it work.

Let's look at the code associated with the command:

choice /c:123x

Choice is the name of the program we are calling, so we type that in. Next, we need to tell the choice program what options the user is choosing from. We use command line parameters to do this, and in this case we are using the C switch. Basically, we type /c to invoke the C switch, then a colon, then the choices. There are many other switches available for use with choice, and we'll get to them in later tutorials.

Next, we have to tell our program what to do when the user presses a button. Well, choice handles part of that for us. It "pauses" the execution of the program until the user presses a button, so we don't need to worry about detecting when it happens.

if errorlevel 4 goto exit
if errorlevel 3 goto pinball
if errorlevel 2 goto keen
if errorlevel 1 goto duke3d

This is the code that tells our program what to do. Basically, it's saying "If the user chooses the 4th option, go to the exit label. If the user chooses the 3rd option, goto the pinball label." And so on. You'll learn about the labels soon.

Notice how we listed them from the last option to the first? That is required in batch files. Never list them from the first option to the last.

You'll also notice that for the exit option, we said "if errorlevel 4", not "if errorlevel x", which was the actual key we used for the option. With the errorlevels, you don't use the actual key - you use the option number. i.e. the 4th option, the 3rd option, the 2nd option, the 1st option.

Next, the goto command tells our program to "go to" or "jump" to another location in the program. And, the word after it (called a label) tells the program exactly where to jump to. Are you starting to see how it all fits together?

Moving on, the line that says ":duke3d" is defining a label. When we say "goto duke3d", we're telling the program to do whatever is on the line after ":duke3d". And the line after that, and the line after that…it goes on. Clever, huh? Next we've got a little message telling the user that Duke Nukem 3D will now run. Of course, if this was all real, this is where we'd put the code telling Duke Nukem 3D to run. But we're not getting into that at the moment.

Next, we've put in a "Press any key to continue…" prompt, and then "goto start". After the user presses any key, the program will look for where it says ":start" (which you should have put in on the very first line), and go back up there. In this example, it'll go back to the main menu.

And that brings us to the end of this second Batch tutorial! In future tutorials we'll learn more about the choice command, and some other batch related commands. Keep an eye out for them :)

Now that you've gotten free know-how on this topic, try to grow your skills even faster with online video training. Then finally, put these skills to the test and make a name for yourself by offering these skills to others by becoming a freelancer. There are literally 2000+ new projects that are posted every single freakin' day, no lie!

Previous Article

Next Article

Chandra's Comment
i like this article i always used to study it
22 Tue Mar 2011
Admin's Reply:

Yeah? That's great. I forgot about this tutorial myself. And ended up using it recently when I created a batch script to backup some key areas of my hard drive.