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 YesGoto End
Echo You selected NoGoto End
:Yes Echo You selected Yes
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]
|/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.)
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.
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.