bat goto SkipIt
The third use of the if command is to test the outcome of the previous command,
as follows:
if errorlevel 0 goto SkipIt
If there was any problem with the statement immediately before this line,
the errorlevel (which is similar to a system-defined variable) will be set to
some nonzero number. The if statement shown here tests for any
errorlevel that is greater than zero; if there was no error, execution will
simply continue to the next command.
Here??™s a revised version of the file-compare example first shown in the
???Command-Line Parameters??? section, earlier in this chapter:
if "%1"=="" goto problem
if "%2"=="" goto problem
if not exist %1 goto problem
if not exist %2 goto problem
562 | Chapter 9: Scripting and Automation
fc %1 %2 >c:\windows\temp\output.txt
if errorlevel 0 goto problem
if not exist c:\windows\temp\output.txt goto problem
notepad c:\windows\temp\output.txt
exit
:problem
echo "There's a problem; deal with it."
This batch file is essentially the same as the original two-line example shown
earlier, except that some error-checking if statements have been added to
make the batch file a little more robust. If you neglect to enter one or both
command-line parameters, or if the files you specify as command-line parameters
don??™t exist, the batch file will display the error message. An even
more useful version might have multiple error messages that more accurately
describe the specific problem that was encountered.
Pages:
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783