1.

Solve : How Use Errorlevel Handler?

Answer»

I have scripts which has many DOS copy / xcopy / move statements.  I do not want to code if %ERRORLEVEL% after each statement which is completely inefficient, anyone have thoughts for the best way in handling this? 

copy statement 1
call errorhandler
copy statement 2
call errorhandler
copy statement 3
....
if errorfile size greater than zero, email error
:end

-----------------------------------------------------------------------------------------------------------------------
In the errorhandler, it would be best if it can CHECK for the errorlevel from the copy | xcopy | move,
if errorlevel equal zero:  return to caller
if errorlevel greater than zero:
   write to some errorfile
   return to caller to EXECUTE next statement....

I would think that as soon as I issue a call statement, the errorlevel would change.  Any ideas? Quote from: et_phonehome_2 on November 20, 2011, 09:02:48 AM

I do not want to code if %ERRORLEVEL% after each statement which is completely inefficient

Do you want to abort the script if a copy statement threw an error ? What do you mean "inefficient"?

I want it to continue onward, eg., execute copy1 then call the errorhandler, then return to execute copy2,.....

copy1
call errorhandler  # will always return to the next stmt whether there is an error or not
copy2
call errohandler   # will always return to the next stmt whether there is an error or not
.....
copyN
So the errorhandler will write a message to a log file and then return?

What message?

Will the errorhandler do ANYTHING else?

Why do you think you need to "call" the error handler?

You see, I don't understand why you think this...

Code: [SELECT]copy command1
call :errorhandler %errorlevel%
copy command 2
call :errorhandler %errorlevel%
...
copy command N
call :errorhandler %errorlevel%
goto end

:errorhandler
if %1 gtr 0 echo %date% %time% Copy error happened! >> error.log
goto :eof

:end
echo script finished

is more efficient than this:

Code: [Select]copy command1
if %errorlevel% gtr 0 echo %date% %time% Copy error happened! >> error.log
copy command 2
if %errorlevel% gtr 0 echo %date% %time% Copy error happened! >> error.log
...
copy command N
if %errorlevel% gtr 0 echo %date% %time% Copy error happened! >> error.log
echo script finished

When running a batch script that has copy | xcopy | move, it is possible to be unaware of an error not until someone tells you they are missing a file. 

You are correct that the error handler probably is not more efficient especially if I just want to OUTPUT that there was an error encountered since I am not zeroing in on a particular error code.
It's always good to get others ideas....  Thanks Salmon Trust.
I do not recall, what would the errorlevel be for the following:

copy path1\file path2\file >> /tmp/log.txt 2>&1
if %errorlevel% gtr 0 echo %date% %time% Copy error happened! >> error.log

the ">>" is just to capture the resultant output from the copy | move | xcopy command...
if the copy failed, but the ">>" was successful, what would the errorlevel be set to?
if the copy was successful, but the ">>" to a file failed, what would errorlevel be set to?


Discussion

No Comment Found