Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How Use Errorlevel Handler  (Read 8505 times)

0 Members and 1 Guest are viewing this topic.

et_phonehome_2

    Topic Starter


    Beginner

    Thanked: 2
    How Use Errorlevel Handler
    « on: November 20, 2011, 09:02:48 AM »
    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?

    Salmon Trout

    • Guest
    Re: How Use Errorlevel Handler
    « Reply #1 on: November 20, 2011, 12:12:19 PM »
    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"?

    « Last Edit: November 20, 2011, 12:37:21 PM by Salmon Trout »

    et_phonehome_2

      Topic Starter


      Beginner

      Thanked: 2
      Re: How Use Errorlevel Handler
      « Reply #2 on: November 20, 2011, 04:18:27 PM »
      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

      Salmon Trout

      • Guest
      Re: How Use Errorlevel Handler
      « Reply #3 on: November 21, 2011, 12:07:14 AM »
      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


      et_phonehome_2

        Topic Starter


        Beginner

        Thanked: 2
        Re: How Use Errorlevel Handler
        « Reply #4 on: November 21, 2011, 02:28:46 AM »
        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.

        et_phonehome_2

          Topic Starter


          Beginner

          Thanked: 2
          Re: How Use Errorlevel Handler
          « Reply #5 on: November 21, 2011, 05:52:10 AM »
          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?