Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Batch Files - Create a Menu to Execute Commands



Batch Files - Create a Menu to Execute Commands

How to Use a Batch File to Create a Command Prompt Menu to Execute Commands
Published by not so gray matter
13 Apr 2010
Published by

How to Use a Batch File to Create a Command Prompt Menu to Execute Commands

information   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   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   Tip
For more about Batch files, read this tutorial:
Microsoft - Batch Files

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






.

17 Jun 2011   #1
ahmedsheraz

Windows 7 Ultimate 32bit
 
 

Nice Sharing buddy

My System SpecsSystem Spec
.

30 Nov 2011   #2
cheebase

XP 32
 
 

The crucial shortcoming of this batch routine is the use of the enter key to complete the command. This was easily avoided by using either the CHOICE or DEBUG utility up until and including any 32 bit environment. These utilities are not included anymore in windows 7 and they can not be used in any 64 bit environment.

Do you have a solution to this dilemma?
My System SpecsSystem Spec
30 Nov 2011   #3
richnrockville

Windows 7 Pro x64 SP1
 
 

Quote   Quote: Originally Posted by cheebase View Post
The crucial shortcoming of this batch routine is the use of the enter key to complete the command. This was easily avoided by using either the CHOICE or DEBUG utility up until and including any 32 bit environment. These utilities are not included anymore in windows 7 and they can not be used in any 64 bit environment.

Do you have a solution to this dilemma?
but it does work in the x64 environment and pressing enter is most likely easier for most people to understand and accomplish. The batch file is great..

Rich
My System SpecsSystem Spec
30 Nov 2011   #4
cheebase

XP 32
 
 

richnrockville: I referred to the inability to run GETKEY or CHOICE in a 64 bit environment. The batch works fine there.
No, I disagree. A single key answer in any menu is the preferred way, forget the enter key... So the question remains: is there a way to accomplish that feat.
My System SpecsSystem Spec
30 Nov 2011   #5
richnrockville

Windows 7 Pro x64 SP1
 
 

Quote   Quote: Originally Posted by cheebase View Post
richnrockville: I referred to the inability to run GETKEY or CHOICE in a 64 bit environment. The batch works fine there.
No, I disagree. A single key answer in any menu is the preferred way, forget the enter key... So the question remains: is there a way to accomplish that feat.
Cheebase, most likely not without a third party program which would have to be installed that the batch file would run. I think that for brevities sake and the ease by which many people could modify the batch file and make it run, this simple batch file method is most likely the cleanest and easiest to understand.

I could write the program in compliled basic with an onkey but I think that this batch file will work for the majority of users.

After all anyone with windows and notepad can make changes and get it done.

Rich
My System SpecsSystem Spec
20 Dec 2011   #6
Cuholvke

Windows 7 Ultimate x64
 
 

I found a turnaround to make the "single key feat". Instead of just using SET or CHOICE, I used both together and it works on x64:

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.
CHOICE /C:1234
IF ERRORLEVEL 1 SET M=1
IF ERRORLEVEL 2 SET M=2
IF ERRORLEVEL 3 SET M=3
IF ERRORLEVEL 4 SET M=4

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
My System SpecsSystem Spec
18 Sep 2012   #7
Fuzzyc00kie

64 bit Windows 7 home premium
 
 

When I try to make a menu open another batch program, it works fine, However, when I try to make an opened batch program open another program, like notepad, it says
The system cannot find the batch label specified -"Label name here".
Any help on the matter?
My System SpecsSystem Spec
Comment

 Batch Files - Create a Menu to Execute Commands




Tutorial Tools



Similar help and support threads for2: Batch Files - Create a Menu to Execute Commands
Windows 7 Tutorial Category
Using notepad(batch programing) to execute a comand at a specific time System Security
Start Command Prompt and automatically execute a few commands Software
Execute copy-cut-paste commands via voice Software
Execute windows commands Customization
How to create a batch file that will move files based on the file type General Discussion
execute commands on power event General Discussion
automatically execute commands when switching to DC Performance & Maintenance

Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 06:41 AM.
Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App
  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33