| 1 |
|
... display output on screen: |
| |
| |
| |
- Standard output (all DOS versions):
ECHO Hello worldNote: This message can be
redirected using ">" or "1>" (without the quotes) |
| |
- Standard error (OS/2, NT):
ECHO Hello world 1>&2Note: This message can
be redirected using "2>" (without the quotes) |
| |
- Directly to the console:
ECHO Hello world >CONNote:
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: |
| |
| |
| |
- Check for known string in the command's output,
using FIND, assuming MS-DOS 6 or later:
SET CONNECT=an
PING 104.12.0.234 ¦ FIND "TTL=" > NUL
IF ERRORLEVEL 1 SET CONNECT=NO
ECHO You have %CONNECT% active connection to the Internet
|
| |
- Search for unknown string, with known
first word in output, using a temporary batch file:
NET USE ¦ FIND "X:" > TEMP.BAT
>> OK.BAT ECHO SET UNC=%%2
CALL TEMP.BAT
IF NOT "%UNC%"=="" ECHO UNC for drive X: is %UNC%
|
| |
- Search for unknown string, with unknown
first word in output, using DATE or TIME and a temporary
batch file:
DIR ¦ FIND "bytes free" > TEMP.TMP
ECHO.>> TEMP.TMP
TYPE TEMP.TMP ¦ DATE ¦ FIND "bytes free" > TEMP.BAT
>> ENTER.BAT ECHO IF NOT "%%3"=="" SET BYTEFREE=%%3
CALL TEMP.BAT
ECHO Total bytes free: %BYTEFREE%
|
| |
- Or use FOR /F in NT:
FOR /F "TOKENS=1,2*" %%A IN ('DATE/T') DO SET TODAY=%%B
ECHO Today is %TODAY%
|
| |
| |
| 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.
|
| |
- Ask for one of a limited number of known characters
(like Yes or No), without using CHOICE (use this method only
as a last resort!):
@ECHO OFF
IF "%1"=="Y" GOTO Yes
IF "%1"=="N" GOTO No
ECHO Yes or No? Press Y or N followed by the Enter key . . .
ECHO @%0 Y > Y.BAT
ECHO @%0 N > N.BAT
GOTO End
:Yes
ECHO You answered Yes
GOTO End
:No
ECHO You answered No
:End
|
| |
- Ask for a string, using COPY CON, assuming ANSI.SYS loaded:
ECHO Enter some input, and press Enter when ready . . .
REM ANSI code to translate Enter key to F6 + Enter key
ECHO [13;0;64;13p
COPY CON USRINPUT.TMP
REM ANSI code to restore Enter key to Enter key only
ECHO [13;13p
CLS
ECHO You typed:
TYPE USRINPUT.TMP
|
| |
|
| |
| |
| 4 |
|
... strip characters from strings: |
| |
| |
| |
- Strip the first character, which happens to be a known
delimiter, using ECHO:
ECHO;ABC
ECHO:ABC
ECHO,ABC
ECHO.ABC
ECHO=ABC
ECHO+ABC
ECHO[ABC
ECHO]ABC
ECHO/ABC
ECHO\ABC
will all display
ABC
|
| |
- Strip any character anywhere in the string using CHOICE
(MS-DOS 6 and later, Windows NT/2000 with the
Resource Kit):
IF "%1"=="Loop" GOTO Loop
SET KEY=[HKEY_LOCAL_MACHINE\Enum\SCSI]
ECHO ]¦ CHOICE /C:;%KEY%;] %0 Loop > TEMP.BAT
CALL TEMP.BAT
ECHO Registry key is: %KEY%
GOTO End
:Loop
REM Strips square brackets from KEY variable
SHIFT
IF "%1"=="]" GOTO End
IF NOT "%1"=="[" SET KEY=%KEY%%1
GOTO Loop
:End
This may not work if your CHOICE version's output cannot be
redirected, as in some Dutch Windows 98 versions.
|
| |
- Strip both leading and trailing double quotes in
Windows 95 /98, using FOR:
FOR %%A IN ("A B C") DO SET STRING=%%A
ECHO.%STRING%
will display
A B C
|
| |
- 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
String: C:\MyDocuments
|
| |
| |
| 5 |
|
... set errorlevels: |
| |
| |
| |
- Set any errorlevel using CHOICE
(MS-DOS 6 and later, Windows NT/2000 with the
Resource Kit):
both
ECHO 8 ¦ CHOICE /C:12345678 /N >NULand
ECHO H ¦ CHOICE /C:ABCDEFGH /N >NULwill
set errorlevel 8 (8th choice).
|
| |
- 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 /Windows
2000, using either COLOR or VERIFY:
COLOR 00will set errorlevel 1
since foreground and background colors
are identical.
No change of color will occur.
The command:
VERIFY OTHER 2> NULwill set
errorlevel 1 too, because OTHER is an invalid
parameter.
|
| |
|
| |
| |
| 6 |
|
... check date and time: |
| |
| |
| |
- Store date (or time) in environment variables
(use date in DATE's output format):
VER ¦ TIME > TEMP.BAT
ECHO SET TIME=%%3>CURRENT.BAT
CALL TEMP.BAT
ECHO It's %TIME% now
|
| |
- Check if a file was created or modified today
(use date in DIR's output format):
DIR %1 ¦ FIND /I "%1" > ~ISMODIF.TMP
ECHO.>> ~ISMODIF.TMP
TYPE ~ISMODIF.TMP ¦ TIME ¦ FIND /I "%1" > ~ISMODIF.BAT
ECHO SET CHKDATE=%%4> ENTER.BAT
CALL ~ISMODIF.BAT
DIR ~ISMODIF.BAT ¦ FIND /I "~ISMODIF.BAT" > ~ISMODIF.TMP
ECHO.>> ~ISMODIF.TMP
TYPE ~ISMODIF.TMP ¦ TIME ¦ FIND /I "~ISMODIF.BAT" > ~ISMODIF.BAT
ECHO SET NOWDATE=%%4> ENTER.BAT
CALL ~ISMODIF.BAT
IF "%NOWDATE%"=="%CHKDATE%" ECHO %1 was created or modified today
|
| |
| |
| 7 |
|
... convert strings to all uppercase or lowercase: |
| |
| |
| |
- Convert to all uppercase (MS-DOS only):
SET STRING=Whatever You Want
SET OLDPATH=%PATH%
PATH %STRING%
SET STRING=%PATH%
PATH %OLDPATH%
SET OLDPATH=
ECHO.%STRING%
This trick works only if the PATH isn't too long and there
is enough environment
space available.
|
| |
- Convert to all uppercase (NT only):
SET STRING=Whatever You Want
IF [%STRING%]==[] GOTO:EOF
SET STRING=%STRING:a=A%
SET STRING=%STRING:b=B%
.
.
.
SET STRING=%STRING:y=Y%
SET STRING=%STRING:z=Z%
SET STRING
|
| |
- Convert to all lowercase (NT only):
SET STRING=Whatever You Want
IF [%STRING%]==[] GOTO:EOF
SET STRING=%STRING:A=a%
SET STRING=%STRING:B=b%
.
.
.
SET STRING=%STRING:Y=y%
SET STRING=%STRING:Z=z%
SET STRING
|
| |
| |
| 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 PING (MS-DOS with TCP/IP client, Windows 95/98/NT/2000):
PING 1.1.1.1 -n 10 -w 1000 >NUL
will delay execution for 10 seconds (10 times 1000 milliseconds),
if and only if 1.1.1.1 is a non-existing IP
address.
|
| |
- 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.
|
| |
- Use Kix' SLEEP command
(Kix must be installed):
>%TEMP%.\SLEEP.KIX ECHO SLEEP 10
KIX32 %TEMP%.\SLEEP.KIX
DEL %TEMP%.\SLEEP.KIX
will delay execution for 10 seconds.
|