When a command is executed in a batch file, in addition to any information it
may display on the screen, it also returns an error level to the batch file
itself. This error level is either errorlevel 0 (meaning no error occurred
and the program worked correctly) or errorlevel 1 (an error occurred and that
section of the batch file did not work correctly). For example, if you
created a simple batch file to format a disk in your floppy (a:) drive like
so:
@echo off
format a:
The format command in the batch file would return errorlevel 0 if there was a
disk in the drive and it was formatted correctly, while if there was not, or the
disk could not be formatted, it would return errorlevel1. By using these
errorlevels and the GOTO command, you can set up your batch files to do
different things depending on whether the commands in them succeed or
fail. For example, you can use the IF command along with the errorlevel
returned by the format command to let the user know whether the operation was
successful or not:
@echo off
format a:
if errorlevel 1 goto error *go
to the :error heading if there's an error in format
echo your disk formatted
successfully *if format returned no error, show this message
goto
end *go to the :end heading to finish the program
:error
echo an
error occurred during formatting
:end
echo
formatting finished
There's no way we could cover all the angles of batch files in a single
guide, so we've got to stop here. If you've been following along, you
should have picked up enough of the fundamentals and possibilities of batch file
creation to start designing your own. Start with modifying the ones we
made here to suit your own backup needs, then progress from there. Have
fun!
Find out about this and many other reviews by
joining the Weekly PCstats.com Newsletter today! Catch all of PCstats
latest hardware reviews right here.