Subscribe

Get the Network Administrators tool pack

Subscribe to our newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them.

Click Here to get your free tools

Recent Posts

Search

Archives

Create a menu for your batch file

Post image for Create a menu for your batch file

by Steve Wiseman on November 30, 2011 · 5 comments

in BAT Files,Tips,Tools


.

I got a question from Justin this week:

“Hi steve,

Have a few scripts that I use for regular admin work. I want to combine them with a simple menu. I was wondering if there was an easy way to make a menu in a batch file.”

Yes there is Justin, and it is actually quite simple.

The SET command is perfect for this. When you call SET /P from the command line it will take a line of input and set it to a variable.

So if you had three options in your batch file like this:

echo Press 1 for Option1
echo Press 2 for Option2
echo Press 3 for Option3
echo Press 4 to Exit

You could put the set command right after it:

set /P OPT=Please make a selection and press enter

This would print the text “Please make a selection..” and then store the users input after they press enter – into the %OPT% variable.

Once that happens we can use a few if statements to conditionally execute.

Here is it all together:

@ECHO OFF
CLS
ECHO 1 - Option 1
ECHO 2 - Option 2
ECHO 3 - Option 4
ECHO 4 - Exit

SET /P OPT=Please make a selection, and press enter:

if %OPT%==1 GOTO OPTION1
if %OPT%==2 GOTO OPTION2
if %OPT%==3 GOTO OPTION3

GOTO EXIT

:OPTION1

ECHO Executing option1!

GOTO EXIT

:OPTION2

ECHO Executing option2!

GOTO EXIT

:OPTION3

ECHO Executing option3!

GOTO EXIT

:EXIT

Save the file as menu.bat and run it

That is all there is to it.

Just change the echo statements to the batch commands you want to execute for each option, and you have an easy to use menu from the command line.

One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools

Related Articles:

{ 5 comments… read them below or add one }

1 Tracy December 7, 2011 at 12:32 pm

Thanks for this. Really appreacite it 🙂

2 Mike Ratlidg December 7, 2011 at 12:42 pm

Thanks for the tip steve. I took this article, and made a script for all the tools on my USB flash drive.

Keep it up!

3 Joe S December 7, 2011 at 12:53 pm

Hi Steve,

Any idea if this will work on Windows NT 4?

4 Steve Wiseman December 7, 2011 at 12:54 pm

NT 4? Whoa.

I don’t even think I have a VMWare image of NT 4.

I believe it will work fine since the set command has worked this way for quite some time…I think this may even work as far back as Windows 95.

5 Roger February 4, 2013 at 7:25 pm

Let me advice to use tool Dr.Batcher to create batch files. Menus are also being made easy with it.

Leave a Comment

Category Links - Windows Forum - Exchange Forum