Batch Files - Create a Menu to Launch Multiple Commands

Information
This tutorial shows the process one goes through when creating a batch file that opens a command prompt menu allowing you to launch commands, programs and more.

I will only be executing programs in this tutorial, but you can use this to execute any DOS command that is available in command prompt.



Tip
Lines typed in the Courier New font should be added to the document.



Opening Notepad

First Open Notepad and Create a New Text Document

Clearing the Command Window

The next thing we'll do is tell the batch file not to show the second command we input. This uses the ECHO OFF command. As mentioned, ECHO OFF only hides the next command, if you wish to hide all commands you must use @ECHO OFF.

ECHO OFF

Once we've turned off display of the next line, we'll issue another command. This command is CLS. CLS clears the current command window to only show a prompt.

CLS

Creating the First Anchor

An anchor is simply a marker which shows the command prompt what to execute when directed to that specific point in the batch file. The first anchor we'll use will be called MENU because when executed it will show the menu of choices. To make MENU an anchor we'll place a : before it.

:MENU

Creating the Menu

Now we'll actually create the menu. This will be the text that the user sees when he/she opens the batch file and it will also be reverted to after executing a command other than exit. The menu will be comprised of ECHO commands. Not only can an ECHO command turn off display of commands, but it can also display whatever you type after it. If you place a space after the ECHO command, it will print whatever you type onto the command window. If you place a . (period) after the ECHO command it will skip a line.

ECHO.
ECHO ...............................................
ECHO PRESS 1, 2 OR 3 to select your task, or 4 to EXIT.
ECHO ...............................................
ECHO.
ECHO 1 - Open Notepad
ECHO 2 - Open Calculator
ECHO 3 - Open Notepad AND Calculator
ECHO 4 - EXIT
ECHO.


Creating the Input Area

Now that we've created the visual menu, we also need to create the place where the user can input his or her choice and execute it. To do this we will use the SET command. This command simply lets the user change one variable or string to another. In other words it will change an input of "1" into an input of GOTO NOTE which will direct the batch file to execute whatever is in the NOTE section. In our SET command we'll give the user 4 choices, one directs the batch file to the NOTE section, another to the CALC section, another to the BOTH section and the last directs the batch file to exit the Window.

SET /P M=Type 1, 2, 3, or 4 then press ENTER:
IF %M%==1 GOTO NOTE
IF %M%==2 GOTO CALC
IF %M%==3 GOTO BOTH
IF %M%==4 GOTO EOF


Creating the Command Sections

Now we'll create the sections of the batch file that are executed by 1, 2, 3 or 4. These sections will be marked by an anchor so that the batch file knows where to start. The sections that execute programs or commands will also be directed to the MENU at the end. If you wished to exit the command prompt instead of falling back on the menu, you could simply place an EXIT command at the end of the section. We will also be using the CD command. This command changes the directory of the command prompt. While not strictly neccesary in this batch file, it will be neccesary if you execute programs outside of the system32 directory. The START command will be used as well. It simply starts the executable. The GOTO command is the last in each section. It tells the batch file to go to a specific anchor. All of the GOTO commands are anchored to the MENU.

:NOTE
cd %windir%\system32\notepad.exe
start notepad.exe
GOTO MENU
:CALC
cd %windir%\system32\calc.exe
start calc.exe
GOTO MENU
:BOTH
cd %windir%\system32\notepad.exe
start notepad.exe
cd %windir%\system32\calc.exe
start calc.exe
GOTO MENU


Saving & Creating the Shortcut

Now that we've finished the batch file, we need to save it. You should save the file as ALL FILES and with a .bat extension. It can be named anything you want. Once you've saved it, you should create a shortcut. This shortcut will allow you to execute the batch file and allow you to change the icon. To change the shortcut's icon simply go to Properties and Change Icon.

Review

In this tutorial we created a batch file which shows a command prompt menu through which you can choose to execute programs. This is only one of many uses for batch files. Files can be copied, DOS commands executed, and more. If you wish to use a batch file to create a menu which will allow you to do more, read through tutorials on DOS commands and using batch files. Below is the entire Document.

ECHO OFF
CLS
:MENU
ECHO.
ECHO ...............................................
ECHO PRESS 1, 2 OR 3 to select your task, or 4 to EXIT.
ECHO ...............................................
ECHO.
ECHO 1 - Open Notepad
ECHO 2 - Open Calculator
ECHO 3 - Open Notepad AND Calculator
ECHO 4 - EXIT
ECHO.
SET /P M=Type 1, 2, 3, or 4 then press ENTER:
IF %M%==1 GOTO NOTE
IF %M%==2 GOTO CALC
IF %M%==3 GOTO BOTH
IF %M%==4 GOTO EOF
:NOTE
cd %windir%\system32\notepad.exe
start notepad.exe
GOTO MENU
:CALC
cd %windir%\system32\calc.exe
start calc.exe
GOTO MENU
:BOTH
cd %windir%\system32\notepad.exe
start notepad.exe
cd %windir%\system32\calc.exe
start calc.exe
GOTO MENU


Tip
For more about Batch files, read this tutorial:
Microsoft - Batch Files

For more DOS commands, read this tutorial:
MS-DOS help and commands