Enterprise Software

The Choice command is back in Windows Vista

Microsoft has put the Choice command back in Windows Vista and made it a bit more powerful. Since the Choice command can come in extremely handy in a lot of situations and because it has been absent so long, Greg Shultz thought it would be worthwhile to revisit this command.

While I use VBScript and Windows Scripting Host quite a bit, I've always been a big fan of batch files! There are just so many system administration tasks that you can accomplish using command line tools and I love being able to combine them in a batch file. Over the years, I've written literally thousands of batch files that perform all sorts of tasks.

While a lot of my batch files just simply run a series of commands from start to finish, some need to prompt a user to make a choice in order to determine which direction the batch file should take. Back in the olden days, getting input from a user in a batch file was next to impossible. But then when Microsoft introduced DOS 6.0 in the early 1990's, they included a new batch file command called Choice, which was designed to give you the ability to make your batch files interactive. At the time, this new feature singlehandedly revolutionized batch files. Once I got used to using the Choice command, I used it every chance I got.

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 to 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.

Well, I recently discovered that Microsoft put the Choice command back in Windows Vista and made it a bit more powerful. Since the Choice command can come in extremely handy in a lot of situations and because it has been absent so long, I thought it would be worthwhile to revisit this command.

In this edition of the Windows Vista Report, I'll examine the Choice command. As I do, I'll show you some examples of situations where it can come in handy.

This blog post is also available in the PDF format in a TechRepublic Download, which includes the example batch file shown in Figure A.

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 the follows the /M parameter becomes the prompt message that the Choice command displays. The [Y,N]? is added by 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 If Errorlevel structure in a batch program, you have to list the numbers in decreasing order.

In a nutshell that's how the Choice command works. Using the additional parameters allow you to create more elaborate Choice commands. Microsoft describes the Choice parameters (Table A) as follows:
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]

Table A

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 bear of a job. However, the task can be easier if 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 verifying 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 so many IPConfig command parameters and many of them are quite long, that remembering them, much less typing them accurately, can be a bear of a job in and of itself. To make using the IPConfig command in Windows Vista a bit easier to use, I've created the batch file shown in Figure A. (The batch file in Figure A is available in the download version of this blog post.)

Figure A

The IPC.bat file with the Choice command makes using the IPConfig command's lengthy parameters easy.
When you run it by typing IPC, this batch file displays a nice menu, as shown in Figure B. 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.

Will the Choice command become part of your toolset?

Do you create and use batch files? Now that the Choice command is back, will you make use of it? Will you download and use the IPC.bat file? Please drop by the Discussion Area and let us hear from you.

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.

19 comments
bernanduntalan
bernanduntalan

if i use xcopy bat.file code what do u think it will run xcopy is i use for backup my file it work it kaya!!!!!!

dvonj
dvonj

What should happen after making a choice? When I run this and make a choice, the cmd window closes.

Chug
Chug

We were a NetWare shop until recently. I had a DOS based NetWare boot CD that displayed a menu and then used the Choice command to allow the user to select what default NDS context they wanted to log into. We also make our own Ghost images for deployment to new computers within the company. We have a boot CD that loads up and then just uses a BAT file to display the list of images and uses the Choice command to prompt the user for which image to load. We've been doing this for years and still do it today.

bus66vw
bus66vw

I plan to make use of this, thanks.

Will Munny
Will Munny

I have tested this batch file with XPsp2 and it does not work on my system. I am using the version of Choice.exe from the Windows 2000 Resource Kit. It returns with an "Error: unrecongnized or incomplete command line" . Is it possible to make this batch file work with XP?

WoW > Work
WoW > Work

I always used SET to give someone a choice in a batch file, through all Windows, 2000 and XP included. set choice= set /p choice=Type the date (mmdd). set choice=%choice:~0,4% ECHO Is the date %choice%? set /p correct=Y or N? set correct=%correct:~0,1% if '%correct%'=='Y' goto md if '%correct%'=='N' goto start if '%correct%'=='y' goto md if '%correct%'=='n' goto start :md c: cd\ md 2008%choice%

ShaunMcG
ShaunMcG

I downloaded this post and the batch file and I think it's great. One comment is that when choosing the first option (ipconfig /all) the batch file exits after flashing on the screen for a second. I am very new to creating batch files or scripting but I added a PAUSE and it worked better for me. Thanks.

DanLM
DanLM

gets a not found. ;o( I have been a unix scripter for, well.. Just say awhile... I need to pick up on the windows scripting for my current job. This is a good thing, not a bad thing... When I see the link to the white paper, I was like. Great, a reference... No reference, content not found. Suggestions? Dan

BALTHOR
BALTHOR

This Goto stuff is a script interpretation.Somebody has a script program in the BIOS/CPU?His figure A is a Notepad and figure B is a cmd exe.The cmd exe probably runs in the BIOS.You could probably find the cmd file in System 32 but how would you start it?It would have to start and run in the BIOS.Figure B looks like a Blue BIOS page.

alex.a
alex.a

CHOICE was always an external command, thus existed as CHOICE.EXE. All one had to do was to copy CHOICE.EXE from a legacy OS such as WinNT into the SYSTEM32 directory of WinXP. I still rely heavily on an application written in BASIC. BASIC.COM has been missing from the OS for years, yet I've always copied it along every time I installed a new OS and it works just fine. Of course, I miss EDLIN so I guess that labels me an incorrigible old-timer. :>)

Mark W. Kaelin
Mark W. Kaelin

Do you still create and use batch files? Now that the Choice command is back in Windows Vista, will you make use of it? Will you download and use the IPC.bat file included with the download version of this post?

GreyTech
GreyTech

I have been using batch files since CP/M days but still learned something new today. It made me read the help on Set and Cmd to find out all sorts of options I had not used that I can in future. Thanks.

kgunnIT
kgunnIT

I previously worked for a circuit board manufacturer and relied very heavily on batch files. My whole job was defined by batch files. However, we never once used a batch file that used the Choice command, everything was written using the Set command. I wonder if the guys who wrote the batch files have even heard of the Choice command.

henriklugn
henriklugn

First of all, your ofcourse right. But thats not all, look at this: :6 cls cls IPConfig /setclassid Goto End Should be :6 cls IPconfig /flushdns Goto End This though is not all errors in the example the author gave. I would say, very sloppy.

bigjoesweeney
bigjoesweeney

Command Prompt is a Command Input Console (a legancy of DOS) not a Basic Input and Output System system. Command Prompt is part of the operating system. http://en.wikipedia.org/wiki/Command_Prompt_(Windows) You can access it by clicking on Start > Run and entering "cmd" in the box and clicking ok. It is used for entering text based commands. The colours are programmable viables in the batch file or as settings of the command prompt console. I hope this helps you understand.

seanferd
seanferd

I always used to import older and missing DOS files into later versions, sometimes having to use Setver to get them to work (Tree, for example). I also liked having Help.com (requiring Qbasic) handy. Why did they strip these commands from later versions of the CLI?

Greg Shultz
Greg Shultz

You are correct in that the figure, which I put together by combining to screen shots, is in error. The screen shot has been corrected! The actual batch file that you download is correct. I apologize for any inconvenience that the error may have caused.

Editor's Picks