IF ERRORLEVEL in batch files

  • Thread starter Thread starter Rick
  • Start date Start date
R

Rick

I want to direct execution to a certain label if XCOPY encounters
*any* error. Can I use

IF ERRORLEVEL>0 goto BadCopy

I'm skeptical about the use of the ">" with IF ERRORLEVEL. Thanks for
any help.
 
Rick said:
I want to direct execution to a certain label if XCOPY encounters
*any* error. Can I use

IF ERRORLEVEL>0 goto BadCopy

I'm skeptical about the use of the ">" with IF ERRORLEVEL. Thanks for
any help.

Sorry, this won't work. The character ">" is a redirection symbol at the
Command Prompt. Here are a few methods:

@echo off
xcopy ..... || goto BadCopy

@echo off
xcopy ....
if %ErrorLevel% GTR 0 goto BadCopy

@echo off
xcopy ....
if ErrorLevel 1 goto BadCopy

The first method is my favorite. I rarely use the third method - it's
convoluted legacy stuff. Note the "%" characters in the second method.
 
Rick said:
@echo off
xcopy ..... || goto BadCopy

Can you explain this? I'm not sure I understand it. Thanks.

===============

There are four uses of the | and & characters in batch files:

1. dir c:\ | find.exe /i "rick"
This is the "pipe" symbol. It "pipes" the output from the "dir" command into
find.exe.

2. dir c:\ & set name=rick
This is the continuation symbol. It lets you run two or more commands with a
single command line.

3. xcopy.exe && goto Error
This is the conditional execution symbol. It executes the command that
follows it only if the %ErrorLevel% of the preceding command was 0.

4. xcopy.exe || goto Error
This is also a conditional execution symbol. It executes the command that
follows it only if the %ErrorLevel% of the preceding command was greater
than 0.
 
Rick said:
Can you explain this? I'm not sure I understand it. Thanks.

Double pipe runs the following command if the first command doesn't
complete successfully (error code greater than 0). See
http://www.microsoft.com/resources/...docs/en-us/ntcmds_shelloverview.mspx?mfr=true

<Quote>

|| [...]

command1 || command2

Use to run the command following || only if the command preceding ||
fails. Cmd.exe runs the first command, and then runs the second command
only if the first command did not complete successfully (receives an
error code greater than zero).
</Quote>

--
Lem -- MS-MVP

To the moon and back with 2K words of RAM and 36K words of ROM.
http://en.wikipedia.org/wiki/Apollo_Guidance_Computer
http://history.nasa.gov/afj/compessay.htm
 
Sorry, this won't work. The character ">" is a redirection symbol at the
Command Prompt. Here are a few methods:

@echo off
xcopy ..... || goto BadCopy

@echo off
xcopy ....
if %ErrorLevel% GTR 0 goto BadCopy

@echo off
xcopy ....
if ErrorLevel 1 goto BadCopy

The first method is my favorite. I rarely use the third method - it's
convoluted legacy stuff. Note the "%" characters in the second method.
==========================================

Is there any place that I can download a ZIP file with all of the XP batch
commands (and definitions)?

I couldn't find anything thru Google.

Thanks for any info.

Jack
 
Rick,
if you look up XCOPY in your Win XP 'Help and Support Centre' on
your 'Start Menu' you will find (near the bottom) a list of EXIT CODES (the
errorlevels) that xcopy uses when it fails due to various reasons. Also a
short explanation and some examples of how to use the ERRORLEVEL command in
batch files.

You can also look up ERRORLEVEL and choose "if" from the results in the
same Help and Support Centre.

==

Cheers, Tim Meddick, Peckham, London. :-)




Rick said:
@echo off
xcopy ..... || goto BadCopy

Can you explain this? I'm not sure I understand it. Thanks.
 
jyazelz said:
Is there any place that I can download a ZIP file with all of the XP
batch commands (and definitions)?

Jack,
[Start] > Run
HH.exe "C:\WINDOWS\help\ntcmds.chm::/ntcmds.htm"
[ok]
 
jyazelz said:
Is there any place that I can download a ZIP file with all of the XP
batch commands (and definitions)?

Jack,
[Start] > Run
HH.exe "C:\WINDOWS\help\ntcmds.chm::/ntcmds.htm"
[ok]
======================

Thanks very much. Just what I was looking for.

Jack
 
Back
Top