WARNING: If you want to learn to write batch script in DOS, stop it right now! Do yourself a favor. Stop reading this tutorial. Unless your are masochist. Please consider other alternative such as Perl. DOS is a pain and the 'language' is not intuitive and not logical.
ECHO
Display string.
REM Display "This is a string." ECHO This is a string. REM Escape special character using ^ REM Display ampersand(&) ECHO ^&
SETLOCAL, ENDLOCAL
SETLOCAL: Localise environment variables under SETLOCAL. ENDLOCAL: Restore previous environment variables that are set before SETLOCAL.
@ECHO OFF REM Value1 SETLOCAL SET value=1 ECHO %value% REM Value2 SETLOCAL SET value=2 ECHO %value% REM Value3 SETLOCAL SET value=3 ECHO %value% REM Back to Value2 ENDLOCAL ECHO %value% REM Back to Value1 ENDLOCAL ECHO %value%
Useful Stuff
- CALL
- Run another batch file.
- cls
- Clear MS-Prompt screen.
- &
- By adding &, you can write multiple instructions on the same line. It will execute instructions sequentially from left to right.
ECHO Hello & ECHO Hello Again
- &&
- Will run the command after && only if the command before && was completed successfully.
ECHO Hello && ECHO I will only run if the previous command was completed successfully.
Comment
- ::
- DOS will not execute any batch file line with twin colons in front of it, nor display it on the monitor screen. After seeing the second colon, DOS ignores anything following and goes to the next line. That is because the colon is an illegal label character.
:: This line is a comment.
- REM or @REM
- REM stands for Remark. DOS does not execute lines starting with REM, but it does read them.
REM This line is also a comment @REM Adding @ in front of REM will not show this command in the DOS prompt.
- &REM or &::
- Unfortunately, using :: or REM alone allow you to only comment the whole line. By adding & in front of REM or ::, you can add a comment at the end of your line of instruction.
@ECHO OFF ECHO Hello1 &:: This is a comment at the end of this line. ECHO Hello2 &REM This is also a comment at the end of this line.
References
- http://www.ss64.com/
- http://www.robvanderwoude.com/news.html
- http://stackoverflow.com/questions/8385454/batch-files-list-all-files-in-a-directory-with-relative-paths
Turn off output
Redirect application to NUL
echo a > NUL