GOTO
To go to a different section in a batch file. You may create different sections by preceding the name with a colon.
Programmers may find this similar to funtions or sub-routines.
@ECHO OFF :FIRSTSECTION ECHO This is the first section PAUSE GOTO SUBSECTION
:SUBSECTION ECHO This is the subsection PAUSE
|
Skip sections of a batch file
@ECHO OFF :ONE ECHO This is ONE, we"ll skip TWO PAUSE GOTO THREE
:TWO ECHO This is not printed
:THREE ECHO We skipped TWO! PAUSE GOTO END :END CLS EXIT
|
Looping with GOTO
:BEGIN REM Endless loop, Help!! GOTO BEGIN
|
Use with CHOICE
IF, IF EXIST, IF NOT EXIST
IF EXIST C:\tempfile.txt DEL C:\tempfile.txt |
IF NOT EXIST C:\tempfile.txt COPY C:\WINDOWS\tempfile.txt C:\tempfile.txt |
Use with "errorlevel"
The generic paramater errorlevel refers to the output another program or command and is also used with the CHOICE structure. If you try and run a command in a batch file and produces an error, you can use errorlevel to accept the returned code and take some action. For example, let"s say you have a batch file that deletes some file.
COPY C:\file.txt C:\file2.txt
|
If "file.txt" doesn"t exist, you will get the error: COULD NOT FIND C:\FILE.TXT. Instead, use a structure like this to create the file, then copy it by accepting the error.
@ECHO OFF :START COPY file.txt file2.txt IF errorlevel 1 GOTO MKFILE GOTO :END
:MKFILE ECHO file text>file.txt GOTO START
:END ECHO Quitting PAUSE
|
an errorlevel of 1 means there was an error, errorlevel of 0 means there was no error. You can see these levels by adding this line after any line of commands:
ECHO errorlevel: %errorlevel% |
Details.
PAUSE
Pauses until the user hits a key.
This s the familiar "Press any key to continue..." message.
REM
Allows a remark to be inserted in the batch .
REM DIR C:\WINDOWS |
Not run as a command |
DIR C:\WINDOWS |
Run as a command |
ECHO
Setting ECHO "on" will the batch process to the screen, setting it to "off" will hide the batch process.
@ECHO OFF |
Commands are NOT ed |
@ECHO ON |
Commands are ed |
ECHO can also be used in batch file to send output to the screen:
@ECHO OFF ECHO. ECHO Hi, this is a batch file ECHO. PAUSE
|
ECHO. sends a blank line.
To echo special characters, precede them with a caret:
ECHO ^<
ECHO ^>
Otherwise you will get an error.
The @ before ECHO OFF suppresses the of the initial ECHO OFF command. Without the @ at the beginning of a batch file the results of the ECHO OFF command will be ed. The @ can be placed before any DOS command to suppress the .
Breaking long lines of code
You may break up long lines of code with the caret ^. Put it at the end of a line, the next line must have space at the begining. Example:
would be:
SET
Use to view or modify environment variables. More.
LASTDRIVE
Sets the last drive in the system.
MSCDEX
Loads the CD-ROM software extensions(drivers), usually so an operating system can be then loaded from CD. See the AUTOEXEC.BAT section for special instructions concerning CD ROM installation. Installing windows from a CD when the CDROM is not yet configured