| 1 |
|
... display output on screen: |
| |
| |
| |
- Standard output (all DOS versions):
ECHO Hello world Note: This message can be
redirected using ">" or "1>" (without the quotes) |
| |
- Standard error (OS/2, NT):
ECHO Hello world 1>&2 Note: This message can
be redirected using "2>" (without the quotes) |
| |
- Directly to the console:
ECHO Hello world >CON Note:
This message cannot be redirected! |
| |
-
| Note: |
|
In NT and OS/2, always either terminate echoed text
with a space when redirecting, or place the echo
command between brackets () and the redirection
outside the brackets, if you are not absolutely
certain that the text will not end with a 2.
Otherwise, you may end up with an unwanted redirection
of standard error ("2>") when you intended to
redirect standard output. |
| |
ECHO This is test
2>filename.ext |
| |
will result in an empty file filename.ext
and the text |
| |
This is
test |
| |
displayed on screen. |
| |
ECHO This is test 2
>filename.ext |
| |
(note the space behind the 2) or |
| |
(ECHO This is test
2)>filename.ext |
| |
will both redirect the text to
filename.ext. |
|
| 2 |
|
... redirect command output to an environment variable: |
| |
| |
| |
|
| |
|
| |
|
| |
|
| |
| |
| 3 |
|
... ask and receive user input: |
| |
| |
| |
- Ask for one single character, using CHOICE
(MS-DOS 6 and later, Windows NT/2000 with the
Resource Kit):
CHOICE /C:ABC Which drive do you want to format
IF ERRORLEVEL 3 GOTO DriveC
IF ERRORLEVEL 2 GOTO DriveB
IF ERRORLEVEL 1 GOTO DriveA
ECHO You pressed the Escape key
GOTO End
:DriveA
FORMAT A:
GOTO End
:DriveB
FORMAT B:
GOTO End
:DriveC
FORMAT C:
:End
or:
CHOICE /C:ABC Which drive do you want to format
IF NOT ERRORLEVEL 1 GOTO Aborted
IF ERRORLEVEL 1 SET DRIVE=A
IF ERRORLEVEL 2 SET DRIVE=B
IF ERRORLEVEL 3 SET DRIVE=C
FORMAT %DRIVE%:
GOTO End
:Aborted
ECHO You pressed the Escape key
:End
Note the order in which the errorlevels are tested: if SET is
used start at errorlevel 1 and increment by 1, if GOTO is used
start at the highest possible errorlevel and decrement by 1.
And remember: errorlevel 0 is possible too, when the user pressed the
Esc key.
|
| |
|
| |
|
| |
|
| |
| |
| 4 |
|
... strip characters from strings: |
| |
| |
| |
|
| |
|
| |
|
| |
- Or use NT's SET's string substitution to replace
or remove characters anywhere in a string:
SET STRING=[ABCDEFG]
SET STRING=%STRING:[=%
SET STRING=%STRING:]=%
ECHO String: %STRING%
will display
String: ABCDEFG
or
SET STRING=[ABCDEFG]
SET STRING=%STRING:[=(%
SET STRING=%STRING:]=)%
ECHO String: %STRING%
will display
String: (ABCDEFG)
or
SET STRING=[ABCDEFG]
SET STRING=%STRING:~1,7%
ECHO String: %STRING%
will display
String: ABCDEFG
or
SET STRING=C:\MyDocuments\
IF "%STRING:~-1%"=="\" SET STRING=%STRING:~0,-1%
ECHO String: %STRING%
will remove a trailing backspace in
Windows 2000 and later (negative numbers
are not supported in NT 4; thanks
for Holger Pielok for pointing out this
omission)
String: C:\MyDocuments
|
| |
| |
| 5 |
|
... set errorlevels: |
| |
| |
| |
|
| |
- Set errorlevel 1 in MS-DOS 6 and up, using FIND:
ECHO A | FIND "B" >NUL
will set errorlevel 1.
|
| |
- Set errorlevel 1 in Windows NT 4/2000/XP, using either COLOR or VERIFY:
COLOR 00 will set errorlevel 1
since foreground and background colors
are identical.
No change of color will occur.
The command:
VERIFY OTHER 2> NUL will set
errorlevel 1 too, because OTHER is an invalid
parameter.
|
| |
|
| |
| |
| 6 |
|
... check date and time: |
| |
| |
| |
|
| |
|
| |
| |
| 7 |
|
... convert strings to all uppercase or lowercase: |
| |
| |
| |
|
| |
|
| |
|
| |
| |
| 8 |
|
... insert a delay: |
| |
| |
| |
- Use CHOICE
(MS-DOS 6 and later, Windows NT/2000 with
Resource Kit):
REM | CHOICE /C:YN /N /T:Y,10 >NUL
will delay execution for 10 seconds in MS-DOS.
TYPE NUL | CHOICE /C:YN /N /T:Y,10 >NUL
will do the same in Windows NT/2000 with CHOICE.EXE from the
Resource Kit.
|
| |
|
| |
- Use SLEEP or TIMEOUT (Windows NT/2000 with
Resource Kit):
SLEEP 10
will delay execution for 10 seconds.
TIMEOUT 10
will wait for 10 seconds or continue when a key is pressed,
whatever comes first.
|
| |
|