Windows

Make the Choice command work for you even in Windows 7

Use the batch-file command Choice to make your batch files interactive. Greg Shultz shows how it can come in handy even in Windows 7.

Back in the old days of computing, I became very adept at creating batch files. It was almost a necessity to be able to automate tasks that would otherwise require a lot of typing at the Command prompt. Of course, I now do most of my task automation using Windows Scripting Host with VBScript and Windows PowerShell. However, there are times when a good old-fashioned batch file comes in really handy. That's why I was glad to see that Microsoft brought back the Choice command in Vista and kept it there in Windows 7.

As you may know, a lot of batch files just simply run a series of commands from start to finish. However, sometimes it is nice to be able to prompt a user to make a choice in order to determine which direction the batch file should take. That's why when Microsoft introduced DOS 6.0 in the early 1990s, they included a new batch-file command called Choice, which was designed to give you the ability to make your batch files interactive.

As the Windows operating system evolved to Windows 95 and then Windows 98, the Choice command came along for the ride. But when Windows 2000 came on the scene, the Choice command was absent. It wasn't included in Windows XP either. While you could download the Choice command and add it to Windows 2000 or Windows XP, it just wasn't the same as having it available as a native command - especially when you were sharing your batch files with other folks.

In this edition of the Windows Desktop Report, I'll examine the Choice command. As I do, I'll show you an example situation where it can come in handy.

Looking at the Choice command

As I mentioned, the power of the Choice command is that it allows you to make your batch files interactive. To see how the Choice command works, let's consider this basic Choice command:

Choice /M "Do you want to continue"

If you type this in a Command Prompt window and press [Enter], you'll see the following prompt

Do you want to continue [Y,N]?

As you can see, the text that follows the /M parameter becomes the message, or prompt, that the Choice command displays. The [Y,N]? is added by the Choice command and is the default list of choices. If you press Y, the Choice command returns a value of 1. If you press N, the Choice command returns a value of 2. These values are assigned to an environment variable named Errorlevel.

With this basic explanation in mind, let's take a look at a more complete example.

Choice /M "Do you want to continue"

If Errorlevel 2 Goto No

If Errorlevel 1 Goto Yes

Goto End
:No

Echo You selected No

Goto End
:Yes

Echo You selected Yes

:End

In this example, I've used the If Errorlevel structure to determine the value assigned to the environment variable, the Goto structure to redirect the batch file execution to the specified label, and the Echo command to display an appropriate results message. You'll also note that when you use the If Errorlevel structure in a batch program, you have to list the numbers in decreasing order.

Parameters

In a nutshell, that's how the Choice command works. Using the additional parameters allows you to create more elaborate Choice commands. Microsoft describes the Choice parameters as follows:

CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]

Parameter

Description

/C    choices Specifies the list of choices to be created. Valid choices include a-z, A-Z, 0-9, and extended ASCII characters (128-254). The default list is "YN."
/N Hides the list of choices in the prompt. The message before the prompt is displayed and the choices are still enabled.
/CS Enables case-sensitive choices to be selected. By default, the utility is case-insensitive.
/T    timeout The number of seconds to pause before a default choice is made. Acceptable values are from 0 to 9999. If 0 is specified, there will be no pause and the default choice is selected.
/D    choice Specifies the default choice after nnnn seconds. Character must be in the set of choices specified by /C option and must also specify nnnn with /T.
/M    text Specifies the message to be displayed before the prompt. If not specified, the utility displays only a prompt.

A real-world example

Now that you have a good idea of how the Choice command works, let's take a look at a real-world example of where the Choice command can simplify the use of a command-line tool in a batch file.

As you know, troubleshooting and diagnosing TCP/IP problems on a Windows network can be a tough job. However, the task can be easier if you use the IP Configuration (IPConfig) command, which is designed to provide you with detailed information on a Windows system's TCP/IP network configuration. This information can be used to help verify network connections and settings and, along with other TCP/IP tools, can assist you in solving TCP/IP problems on a Windows network.

Unfortunately, there are numerous IPConfig command parameters, and many of them are quite long, so remembering them, much less typing them accurately, can be a bear of a job in and of itself. To make using the IPConfig command a bit easier, I've created the batch file shown in Figure A. (You can download the batch file if you prefer.) The strange-looking characters that you see are actually special characters that I copied from Character Map and are configured in such a way as to create a nice window -- like a border, as you'll see.

Figure A

The IPC.bat file with the Choice command makes using the IPConfig command's lengthy parameters easy to access.
When you run it by opening a Command Prompt window and typing IPC, this batch file displays a nice menu, as shown in Figure B, and then using the Choice command allows you to easily select and run the most common IPConfig command lines. You just type a number, and the command runs.

Figure B

Once the menu displays, you just type a number, and the appropriate IPConfig command line runs.

What's your take?

Do you create and use batch files on a regular basis? Now that the Choice command is back, will you make use of it? Will you download and use the IPC.bat file? As always, if you have comments or information to share about this topic, please take a moment to drop by the TechRepublic Community Forums and let us hear from you.

Also read:

About

Greg Shultz is a freelance Technical Writer. Previously, he has worked as Documentation Specialist in the software industry, a Technical Support Specialist in educational industry, and a Technical Journalist in the computer publishing industry.

27 comments
bitdoctor
bitdoctor

Old, but good posting. But how does Choice help for > 9 selections?

Seems like it picks based on the first key pressed; i.e., you can only have 9 choices; or am I missing something. Better would be to have an example with 10 choices, if this command works for > 9.

Thoughts? Comments?

Cmd_Line_Dino
Cmd_Line_Dino

You can simplify a section of the script by replacing the 9 "IF ERRORLEVEL" statements with one GOTO ... ----- Choice /C 123456789 /M "Which IPConfig option do you want to use" Goto %Errorlevel% ----- to be complete a one line range check could be added before the goto ... ----- if %Errorlevel% LSS 1 ( goto ERR ) else ( if %Errorlevel% GTR 9 goto ERR ) ----- The above works in XP and above. Not sure about prior to XP.

gmaran23
gmaran23

Good article, and I liked the way the options are displyed with the charactor map. I see it for the first time.

Zenith545
Zenith545

Since Microsoft decided NOT to include CHOICE in XP, I have become familiar with the SET /P command: SET /P variable=[promptString] Great for creating mini-menus.

bici.nando
bici.nando

In batch files the symbol = is never taken into account; furthermore the line: if errorlevel X is not interpreted as if errorlevel=X but instead: if errorlevel>=X This motivates the need to list the numbers in decreasing order.

bott
bott

Yeah I still use them, got a couple of old ones used for generating default company configurations for Cisco devices which where originally created about 8 years ago and I have been maintaining ever since, it gets used on average about 3 times a week (and not just by me) I keep updating it every so often. I have noticed that at the same time as introducing things like PowerShell, MS have been making significant improvements to Batch, If you are familiar with Batch you should check out the improvements. As an example Batch now supports code blocks using parenthesis, so wizard57's code can be improved thusly... (Sorry for the plagiarism, I'm being lazy) Echo off :Comment Choice /M "Do you want to make a comment?" If Errorlevel 2 Goto ( Echo You selected No, you have nothing to add Goto End ) If Errorlevel 1 ( Echo You selected Yes, Are you sure? Goto Comment ) :end Other new features present in windows 7 that you should check out before abandoning Batch are For command can split an input for processing, as well as reading through a file line by line processing each line. Set command now has the ability to do basic calculations Timeout is the windows equivalent of sleep.

wyattharris
wyattharris

I still remember one of my students telling me "DOS is Boss" after he finally got some real world experience. I haven't used CHOICE in a while but I do have my USB master Ghost boot key and it's got a pretty extensive MENUITEM boot menu to automate loading nic drivers for all of our different PCs. Other than that, I've migrated most everything to WSH and Powershell.

sparker
sparker

I started on PCs when DOS 1.0 came out, so I have used batch files for years to do almost everything. I have to laugh now, when I pull up a command box to type in a command-line or create a quick bat file. Most of the techs I work with have never seen a bat file, much less make one. I usually hear an incredulous "You know how to do that???"

mike.jantzer
mike.jantzer

But your test runs just fine and choice /? works also

DoubleBarrel
DoubleBarrel

Used a program called batcom for years, it would compile a batch file into an executable and allow you to put it on a customers computer without fear of someone modifying it without permission. It also had additional commands that you could add. Oh for the "Good old days"???

murcorp
murcorp

I tried a few searches on microsoft.com to "... download the Choice command and add it to ... XP", but no luck for me. Could you point to where I might be able to download it? Thanks in advance, and have a great day.

IronPh
IronPh

- add a "Pause" after :End. Otherwise Windows will close the command window after you press a key.

david.thomas
david.thomas

You'd be amazed what you can do in PowerShell... time to let go of batch files!

CharlieSpencer
CharlieSpencer

I was unaware of 'CHOICE', but I can see myself using it in the future.

Oglesby1205
Oglesby1205

I attempted to download this from the "downloads" page and got only the URL for the topic.

Ben_in_CA
Ben_in_CA

I use batch files for various uses regularly - such as copying data from one drive to another with various options set, disabling software, displaying IP information, etc. They are quite powerful and fairly simple to use.

wizard57m-cnet
wizard57m-cnet

Echo off :Comment Choice /M "Do you want to make a comment?" If Errorlevel 2 Goto No If Errorlevel 1 Goto Yes :No Echo You selected No, you have nothing to add Goto End :Yes Echo You selected Yes, Are you sure? Goto Comment :End Hehe!

Mark W. Kaelin
Mark W. Kaelin

Do you create and use batch files on a regular basis? Are you using the Choice command?

Cmd_Line_Dino
Cmd_Line_Dino

Since at least XP you can do this in batch... if %Errorlevel% EQU 1 goto 1 the full list of compare ops... EQU - equal NEQ - not equal LSS - less than LEQ - less than or equal GTR - greater than GEQ - greater than or equal

Cmd_Line_Dino
Cmd_Line_Dino

Actually Windows 7 SP1 does not have any BATCH language enhancements compared to Windows XP SP3. All the features you mentioned exist in XP. I compared via Windiff the documentation for XP and WIN7. These are the only BATCH related changes from XP to Win7... New dynamic environment variable %HIGHESTNUMANODENUMBER% The following commands have new switches COPY switch for symbolic links XCOPY switches for symbolic links and unbuffered I/O. Recommended for very large files DIR switches for alternate data streams of files and Not content indexed Files and Reparse Points DEL switches for Not content indexed Files and Reparse Points START switches for AFFINITY and NODE ( aka NUMA ) What documentation did I compare? If any interest I can post the details.

gmaran23
gmaran23

C:\Users\maran>pause /? Suspends processing of a batch program and displays the message Press any key to continue . . . Handy trick.

P.V.S.
P.V.S.

thanks. just run it several times and was trying to figure out how to stop it from closing. thanks again.

Craig_B
Craig_B

Yes, PowerShell is a great tool however there are times when Batch files do the trick. It's about picking the right tool for the job, which could be Batch files, PowerShell, VB Script, GUI application, etc.

Plant Doctor
Plant Doctor

I agree. As often as I use bat files I had not known about this command. I, too, can see myself using it in the future. Great tip.

wizard57m-cnet
wizard57m-cnet

Prior to that I used a small BATCH file helper called "ASK.COM", it even had color prompts! I made an entire menu for my old DOS 6.22 IBM PS2 Model 5500SX using CHOICE, along with a couple of other tools. So simple that even my kids, who were like 10yrs, 5 yrs, and 3 yrs old at the time, could start the games they wanted to play. ps---thanks for the +1!

Mark W. Kaelin
Mark W. Kaelin

I just clicked the link and it downloaded okay. Please try again and I apologize for the trouble.