This is a collection of batch techniques and tips dating back to the MS-DOS and Windows NT 4 era.
Some of them may have become obsolete by now...
ECHO Hello world
Note: | This message can be redirected using > or 1> |
ECHO Hello world 1>&2
Note: | This message can be redirected using 2> |
ECHO Hello world >CON
Note: | This message cannot be redirected! |
Note: | In NT 4 and OS/2, always either terminate echoed text with a space when redirecting, or place the echo command between parenthess () 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 following the 2) or (ECHO This is test 2)>filename.ext will both redirect the text to filename.ext |
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
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%
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%
FOR /F "tokens=1 delims= " %%A IN ('DIR *.DOC ^| FIND "File(s)"') DO SET NumDocs=%%A
ECHO %NumDocs% MS-Word documents found
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
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
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.@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
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
←
, which is an Esc character (ASCII 27 or 0x1B)ECHO;ABC
ECHO:ABC
ECHO,ABC
ECHO.ABC
ECHO=ABC
ECHO+ABC
ECHO[ABC
ECHO]ABC
ECHO/ABC
ECHO\ABC
ABC
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
FOR %%A IN ("A B C") DO SET STRING=%%A
ECHO.%STRING%
A B C
SET STRING=[ABCDEFG]
SET STRING=%STRING:[=%
SET STRING=%STRING:]=%
ECHO String: %STRING%
String: ABCDEFG
SET STRING=[ABCDEFG]
SET STRING=%STRING:[=(%
SET STRING=%STRING:]=)%
ECHO String: %STRING%
String: (ABCDEFG)
SET STRING=[ABCDEFG]
SET STRING=%STRING:~1,7%
ECHO String: %STRING%
String: ABCDEFG
SET STRING=C:\MyDocuments\
IF "%STRING:~-1%"=="\" SET STRING=%STRING:~0,-1%
ECHO String: %STRING%
String: C:\MyDocuments
ECHO 8 | CHOICE /C:12345678 /N >NUL
ECHO H | CHOICE /C:ABCDEFGH /N >NUL
ECHO A | FIND "B" >NUL
COLOR 00
VERIFY OTHER 2> NUL
VER | TIME > TEMP.BAT
ECHO SET TIME=%%3>CURRENT.BAT
CALL TEMP.BAT
ECHO It's %TIME% now
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
SET STRING=Whatever You Want
SET OLDPATH=%PATH%
PATH %STRING%
SET STRING=%PATH%
PATH %OLDPATH%
SET OLDPATH=
ECHO.%STRING%
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
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
REM | CHOICE /C:YN /N /T:Y,10 >NUL
TYPE NUL | CHOICE /C:YN /N /T:Y,10 >NUL
PING 1.1.1.1 -n 10 -w 1000 >NUL
SLEEP 10
TIMEOUT 10
>%TEMP%.\SLEEP.KIX ECHO SLEEP 10
KIX32 %TEMP%.\SLEEP.KIX
DEL %TEMP%.\SLEEP.KIX
page last modified: 2011-03-04; loaded in 0.0022 seconds