Windows 7 / Getting Started

Using the Choice Command

The Choice command lets you add interactive processing to batch files. Whether you use this option depends on the kind of automation you want to add to your processing tasks. Most of the automation you create for optimization tasks won't require any kind of interactivity because you already know how you want the task performed based on experience you obtained performing the task manually. However, sometimes you do need to add some interactivity. For example, you might run the command one way on Friday and a different way the rest of the week. The Choice command can also help you add safeguards that ensure the user understands the ramifications of performing a certain task before they actually do it. Vista and Server Core change the Choice command significantly, breaking many batch files. The Vista and Server Core form of the Choice command differs not in arguments, but in how you combine those arguments at the command line. Here's the command line for Vista and Server Core:

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

The changes make the command clearer, but they break existing batch files in a way that you can't easily fix. The new /CS command line switch lets you make choices case sensitive, so you can have 26 additional menu choices. However, notice that /T no longer takes both a default option and a timeout value. The new form requires that you provide a choice using the /D command line switch instead. You must also provide the /M command line switch to specify optional text. The following sample code performs the same task, but the first form works in Windows XP and earlier, while the second form works in Vista and Server Core.

Old Choice Command Form
CHOICE /C:N /N /T:N,15
Vista and Server Core Choice Command Form
CHOICE /C N /N /T 15 /D N
NOTE: Vista and Server Core provide alternatives for the Choice command. The TimeOut utility provides a specific timeout value without requiring the user to make a choice. You can learn more about this utility in the "Using the TimeOut Utility" section of the tutorial. The WaitFor utility lets you use signaling between systems or applications on the same system. One application sends a signal and another reacts when it receives the signal. You can learn more about this utility in the "Using the WaitFor Utility" section of the tutorial.

When you use Choice by itself, it displays a simple [Y,N] prompt that doesn't accomplish much unless you also provide an Echo command to describe what the user should say yes or no to. Normally, you'll combine the Choice command with one or more arguments. Listing-1 shows a simple example of the Choice command at work.

Listing-1: Using the Choice Command
Echo Off

REM Keep repeating until the user enters E.
:Repeat

REM Display the choices.
Choice /C DCE /N /T 15 /D E /M "Choose an option (D)isplay, (C)opy, or (E)nd."
REM Act on the user choice.
If ErrorLevel 3 Goto End
If ErrorLevel 2 Goto Copy
If ErrorLevel 1 Goto Display

REM Copy the file.
:Copy
Echo You chose to copy the file.
Goto Repeat

REM Display the file.
:Display
Echo You chose to display the file.
Goto Repeat

REM End the batch processing.
:End
Echo Goodbye
Echo On

The code begins by creating a repeat label so the batch file continues working until the user specifically stops it. Next, the code uses the Choice command to display the choices to the user. The /C switch tells Choice that the valid options are D, C, or E instead of the default Y or N. Because the text specifically defines the characters that the batch file expects, the batch file uses the /N switch to suppress displaying the valid key choices on the command line. The /T command line switch tells Choice to automatically choose E after 10 seconds. The /D command line switch provides the default choice of E. Finally, the /M command line switch provides the message displayed to the user.

Although this batch file doesn't actually do anything with a file, it shows how you'd set up the batch file to process the user choice. Notice that the batch file uses the ErrorLevel clause of the If statement to detect the user choice. The ErrorLevel clause detects every choice lower than the user selection, so you must place the values in reverse order, as shown. In addition, you must specifically set the batch file to go to another location because it will process all other statements after the current error level.

The processing code simply displays a string telling you what choice the user made. Normally, you'd add tasks that the batch file should perform based on the user's selection. Notice that the copy and display selections tell the batch file to go back to the Repeat label. This is the most common technique for creating a menu loop in a batch file. The batch file ends by telling the user goodbye and turning echo back on.

Using the Echo Command

The command line uses the term echo to describe the process where the system echoes (repeats) every command in a batch file to the command line. Echo provides a means of seeing which command the system is processing. However, echo can become confusing for users who aren't aware of or don't care about the commands that are executing. In addition, echo can disrupt visual effects, such as menu systems, that you create. The Echo command has two forms. The first form

ECHO [{ON | OFF}]

displays the echo status when you don't include any arguments. The ON argument turns on echo so you can see the commands, and the OFF argument turns off echo so you can create visual effects. You can precede the Echo command with the @ sign so it doesn't appear as one of the commands. @Echo OFF would turn echo off without displaying the echo command at the command prompt. The second form of echo

ECHO [message]

lets you display a message. Simply type the text you want to see after the Echo command. In this case, the system won't display the Echo command, just the message you want to display. Don't use the @ sign with this form of the Echo command or the user won't see the message.

Using the Exit Command

Most people associate the Exit command with closing the current command window. Using Exit alone will close the command window. However, you can also use this command within a batch file to exit the batch file. To perform this task, you must use one or both of the following optional Exit arguments.

/B Specifies that you want to exit a batch file, rather than the current command line session. If you don't specify this command line switch, the command window closes, even when you issue the Exit command from a batch file.

ExitCode Defines an exit code for the batch file. The default exit code is 0, which normally signifies success. You can use exit codes to alert the caller to errors or special conditions. The exit codes aren't defined by the system, so you can define any set of exit codes that you deem necessary for your application.

Using the ForFiles Utility

The ForFiles utility provides a means of looping through a list of files and performing actions on those files one at a time. For example, you might want to process all files that someone has changed since a certain date. In most respects, this loop method works precisely the same as the For command described in the "Using the For Command" section of the tutorial. This command uses the following syntax:

FORFILES [/P pathname] [/M searchmask] [/S] [/C command] [/D [+ | -] {MM/dd/yyyy | dd}]

The following list describes each of the command line arguments.

/P pathname Specifies the starting point for a search. The path is the starting folder in the search. The default setting uses the current directory as the starting point.

/M searchmask Defines a search mask for the files. You can use the asterisk (*) and question mark (?) as wildcard characters, just as you would when using the Directory command. The default setting searches for all files in the target directory.

/S Searches all of the subdirectories of the specified directory.

/C command Specifies the command you want to execute for each file. Always wrap the command in double quotes to ensure it isn't interpreted as part of the ForFile command. The default command is "cmd /c echo @file". Always precede internal command processor command with cmd /c. The following list describes the variables that you can use as part of the command.

@file Returns the name of the file, including the file extension.
@fname Returns the name of the file without the extension.
@ext Returns only the file extension.
@path Returns the full path of the file. This information includes the drive as well as the actual path.
@relpath Returns the relative path of the file. The relative path begins at the starting folder.
@isdir Specifies whether the file type is a directory. True indicates a directory entry.
@fsize Indicates the size of the file in bytes.
@fdate Indicates the date that someone last modified the file.
@ftime Indicates the time that someone last modified the file.
TIP: You can include special characters in a command by using the 0xHH format where HH is a hexadecimal number. For example, you can specify a tab by typing 0x09. /D date Selects files that have a last modified date within the specified range. You specify a specific date using the month/day/year (mm/dd/yyyy) format. Add a plus sign if you want files after the specified date or a minus sign if you want files before the specified date. For example, /D -01/01/2008 would select all files modified before January 1, 2008. You can also specify a relative date by providing a positive or negative number. For example, /D -7 would select all files modified within the last seven days. The /D command line switch accepts any number between 0 and -32,768.
[Previous] [Contents] [Next]