9. Using the Pause Command
The Pause
command stops batch file execution to give the user time to react to a
particular need. For example, if you need the user to change media to
complete the batch file, you could use the Echo command to tell the user about the need and then use the Pause command to tell the user to press any key when the media exchange is complete.
10. Using the Prompt Command
The Prompt
command changes the command line prompt. For example, instead of the
usual drive letter, directory, and greater than sign, you could use the
time and date as a prompt. In fact, the prompt can contain any text you
want. To change the prompt, simply type Prompt,
followed by the text you want to display, and press Enter. The
following list defines the special characters you can use as part of the
command prompt.
- $A
& (Ampersand)
- $B
| (Pipe)
- $C
( (Left parenthesis)
- $D
Current date
- $E
Escape code (ASCII code 27)
- $F
) (Right parenthesis)
- $G
> (Greater than sign)
- $H
Backspace (erases previous character)
- $L
< (Less than sign)
- $N
Current drive
- $P
Current drive and path
- $Q
= (Equals sign)
- $S
(Space)
- $T
Current time
- $V
Windows version number
- $_
Carriage return and linefeed
- $$
$ (Dollar sign)
You can access two
additional formatting characters when you have command extensions
enabled. The following list describes these two additions.
- $+
Displays zero or
more plus sign characters depending on the depth of the PushD utility
directory stack. The display shows one character for each level you've
pushed onto the stack.
- $M
Displays the
remote name associated with the current drive letter. If this is a local
drive, then the system displays an empty string.
11. Using the Rem Command
The Rem (Remark)
command lets you add comments to your batch files. Given that batch
files often use difficult to read coding sequences and that you'll
probably want to modify them at some point, lots of comments are
advisable. In fact, you'll want to add at least one comment for each
complex line of code in your batch file. Many people have lost use of
interesting and helpful batch files because they contain complex code
that becomes unreadable after the initial writer forgets what the code
means.
12. Using the Shift Command
A batch file supports a maximum of 10 command line arguments numbered %0 through %9. However, you might run into situations where you need more than 10 command line arguments. The Shift
command can help you shift in these additional arguments. The new
arguments replace existing arguments. In fact, all of the arguments are
shifted one position, so the argument in %1 now appears in %0. Unfortunately, the argument in %0 is shifted out so that it's no longer accessible.
You can retain some older arguments in memory when you have command extensions enabled. The Shift command will accept a numeric argument that tells where to begin shifting arguments. For example, when the command appears as Shift /2, the values in %0 and %1 are unaffected. However, Shift will shift the arguments starting with %2 so that %2 now contains the value from %3.
13. Using the TimeOut Utility
The TimeOut utility
provides a unique feature in that you can tell it to wait for a
specified time period no matter what the user does. Consequently, unlike
the Choice command, when you tell
TimeOut to wait 30 seconds, it waits the entire time period even if the
user presses a key. In addition, the TimeOut utility doesn't display a
message for the timeout, so you can use this utility where silence is
necessary (such as a background task). This command uses the following
syntax:
TIMEOUT [/T] timeout [/NOBREAK]
The following list describes each of the command line arguments.
/T
timeoutSpecifies the
timeout value. The command line switch is optional. You may specify any
value from −1 to 99999 seconds. A value of −1 means that the utility
waits indefinitely for a key press. The utility won't allow you to
combine a value of −1 with the /NOBREAK command line switch since that would effectively lock the system.
- /NOBREAK
Prevents the utility from recognizing key presses. The TimeOut utility waits for the specified time period before it exits.
14. Using the WaitFor Utility
The WaitFor utility enables
communication between processes. You can send a signal from one
application to another. In fact, you can use this feature for signaling
between batch files. When using this utility, you start the receiving
application first and tell it to wait for the signal. The sender then
sends the signal when it's ready. This command uses the following
syntax:
Sender Syntax:
WAITFOR [/S system [/U [domain\]user [/P [password]]]] /SI signal
Receiver Syntax:
WAITFOR [/T timeout] signal
The following list describes each of the command line arguments.
/S
systemSpecifies the remote system that you want to check. In most cases, you'll also need to supply the /U and the /P command line switches when using this switch.
/U
[domain\]userSpecifies the
username on the remote system. This name may not match the username on
the local system. You'll need to supply a domain name when working with a
domain controller.
/P
[password]Specifies the
password for the given user. You can provide the command line switch
without specifying the password on the command line in clear text. The
system prompts you for the password. Using this feature can help you
maintain the security of passwords used on your system.
/SI
Sends the requested signal across the network.
signal
Specifies the signal to send or receive. The signal is a simple string value such as StartSetup.
A system can wait for multiple unique signals. The maximum signal name
length is 255 characters. You may use a–z, A–Z, 0–9, and any ASCII
character code in the range 128–255 for the string value. The string
value can't contain special characters or spaces.
/T timeout
Defines the amount
of time to wait for the signal. You can specify any value from 1 to
99,999 seconds. The default setting waits an infinite amount of time.