This action might not be possible to undo. Are you sure you want to continue?
...display output on screen
• Standard output (all DOS versions):
ECHO Hello world
This message can be redirected using > or 1> Standard error (OS/2, NT):
ECHO Hello world 1>&2
This message can be redirected using 2> Directly to the console:
ECHO Hello world >CON
This message cannot be redirected!
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
No te: 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
redirect command output to an environment variable: • Check for known string in the command's output.234 | FIND "TTL=" > NUL IF ERRORLEVEL 1 SET CONNECT=NO ECHO You have %CONNECT% active connection to the Internet • Search for unknown string. assuming MS-DOS 6 or later: SET CONNECT=an PING 104.BAT >> OK.. using FIND.TMP ECHO. with known first word in output.BAT ECHO Total bytes free: %BYTEFREE% • Or use FOR /F in NT 4+: FOR /F "tokens=1 delims= " %%A IN ('DIR *. using DATE or TIME and a temporary batch file: DIR | FIND "bytes free" > TEMP.TMP TYPE TEMP. using a temporary batch file: NET USE | FIND "X:" > TEMP. 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 .BAT ECHO SET UNC=%%2 CALL TEMP..TMP | DATE | FIND "bytes free" > TEMP...BAT IF NOT "%UNC%"=="" ECHO UNC for drive X: is %UNC% • Search for unknown string..BAT ECHO IF NOT "%%3"=="" SET BYTEFREE=%%3 CALL TEMP. Read more about the use of FOR /F and its tokens and delimiters. with unknown first word in output.0.>> TEMP.DOC ^| FIND "File(s)"') DO SET NumDocs=%%A ECHO %NumDocs% MS-Word documents found Thanks for Rob Davis who noticed that the example I used before didn't work with all regional settings.ask and receive user input: • Ask for one single character. .BAT >> ENTER. using CHOICE (MS-DOS 6 and later.12.
. . .13p COPY CON USRINPUT. using COPY CON.TMP REM ANSI code to restore Enter key to Enter key only ECHO ←[13. ECHO @%0 Y > Y.13p CLS ECHO You typed: TYPE USRINPUT. 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 . REM ANSI code to translate Enter key to F6 + Enter key ECHO ←[13.TMP .SYS loaded: ECHO Enter some input.BAT ECHO @%0 N > N.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.0.64. . • Ask for one of a limited number of known characters (like Yes or No). assuming ANSI. if GOTO is used start at the highest possible errorlevel and decrement by 1. when the user pressed the Esc key. And remember: errorlevel 0 is possible too. and press Enter when ready .BAT GOTO End :Yes ECHO You answered Yes GOTO End :No ECHO You answered No :End • Ask for a string.
BAT CALL TEMP.strip characters from strings: • Strip the first character.BAT ECHO Registry key is: %KEY% GOTO End :Loop REM Strips square brackets from KEY variable SHIFT IF "%1"=="]" GOTO End ..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. which happens to be a known delimiter.%KEY%.ABC ECHO.'s tip # 0323: How can I get a batch file to prompt me for parameters? Windows 2000 / XP: Use any of the procedures mentioned above or use Windows 2000's SET /P .Note the ←. Windows NT/2000 with the Resource Kit): IF "%1"=="Loop" GOTO Loop SET KEY=[HKEY_LOCAL_MACHINE\Enum\SCSI] ECHO ]| CHOICE /C:. using ECHO: ECHO.. which is an Esc character (ASCII 27 or 0x1B) • Obtaining user input in Windows: Windows 95 / 98: Use any of the procedures mentioned above or a real GUI style box as explained in Tom Lavedas' New and Improved Data Input Routine! Windows NT 4: Use any of the procedures mentioned above or a real GUI style box as explained in Duke Communications International.] %0 Loop > TEMP.ABC ECHO:ABC ECHO. Inc.
IF NOT "%1"=="[" SET KEY=%KEY%%1 GOTO Loop :End This may not work if your CHOICE version's output cannot be redirected.7% ECHO String: %STRING% will display String: ABCDEFG . as in some Dutch Windows 98 versions.%STRING% will display: ABC • 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. • Strip both leading and trailing double quotes in Windows 95 /98. using FOR: FOR %%A IN ("A B C") DO SET STRING=%%A ECHO.
. Windows NT/2000 with the Resource Kit): both ECHO 8 | CHOICE /C:12345678 /N >NUL and ECHO H | CHOICE /C:ABCDEFGH /N >NUL will set errorlevel 8 (8th choice). The command: VERIFY OTHER 2> NUL will set errorlevel 1 too. • Use ERRORLVL. No change of color will occur. • Set errorlevel 1 in Windows NT 4/2000/XP.EXE by OzWoz Software. because OTHER is an invalid parameter. • Set errorlevel 1 in MS-DOS 6 and up.0 by Jim Elliott for MS-DOS 5 or earlier versions. thanks for Holger Pielok for pointing out this omission): String: C:\MyDocuments .or: SET STRING=C:\MyDocuments\ IF "%STRING:~-1%"=="\" SET STRING=%STRING:~0. or SETERLEV.set errorlevels: • Set any errorlevel using CHOICE (MS-DOS 6 and later.-1% ECHO String: %STRING% will remove a trailing backspace in Windows 2000 and later (negative numbers are not supported in NT 4. . using either COLOR or VERIFY: COLOR 00 will set errorlevel 1 since foreground and background colors are identical. using FIND: ECHO A | FIND "B" >NUL will set errorlevel 1..COM 1.
BAT CALL TEMP.>> ~ISMODIF...TMP ECHO.TMP | TIME | FIND /I "~ISMODIF.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 ~ISMODIF.BAT" > ~ISMODIF.BAT DIR ~ISMODIF.TMP ECHO.TMP TYPE ~ISMODIF. • 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 .TMP TYPE ~ISMODIF.BAT CALL ~ISMODIF...BAT | FIND /I "~ISMODIF.TMP | TIME | FIND /I "%1" > ~ISMODIF.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.>> ~ISMODIF.BAT ECHO SET CHKDATE=%%4> ENTER.BAT IF "%NOWDATE%"=="%CHKDATE%" ECHO %1 was created or modified today .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.BAT" > ~ISMODIF.BAT ECHO SET NOWDATE=%%4> ENTER.
1 is a non-existing IP address. • Use KiXtart's SLEEP command (KiXtart must be installed): >%TEMP%.. • Use SLEEP or TIMEOUT (Windows NT/2000 with Resource Kit): SLEEP 10 will delay execution for 10 seconds.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 .1. • Use PING (MS-DOS with TCP/IP client.1.1..\SLEEP.1.\SLEEP.\SLEEP.1 -n 10 -w 1000 >NUL will delay execution for 10 seconds (10 times 1000 milliseconds).KIX DEL %TEMP%.KIX will delay execution for 10 seconds. Windows 95/98/NT/2000): PING 1. TYPE NUL | CHOICE /C:YN /N /T:Y.insert a delay: • Use CHOICE (MS-DOS 6 and later. Windows NT/2000 with Resource Kit): REM | CHOICE /C:YN /N /T:Y. if and only if 1.EXE from the Resource Kit. TIMEOUT 10 will wait for 10 seconds or continue when a key is pressed.KIX ECHO SLEEP 10 KIX32 %TEMP%. whatever comes first.10 >NUL will do the same in Windows NT/2000 with CHOICE. .10 >NUL will delay execution for 10 seconds in MS-DOS.