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