Results 1 to 3 of 3

Thread: [RESOLVED] Catching errorlevel from a batch file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Resolved [RESOLVED] Catching errorlevel from a batch file

    I know this is possible by using the GetExitCodeProcess API, the problem with it is that it just catches the code of the ending command... for example, a batch file with this code:

    Code:
    @echo off
    copy c:\dummy.jpeg c:\
    The function catches the errorlevel correctly, but if I add "echo errorlev = %ERRORLEVEL%" the GetExitCodeProcess returns a 0 as the last line isn't generating any error...

    I've tried catching the exit code doing a loop this way:

    vb Code:
    1. ProcessId = Shell("c:\a.bat", 1)
    2.     hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)
    3.  
    4.     Do
    5.  
    6.         Call GetExitCodeProcess(hProcess, exitCode)
    7.         DoEvents
    8.    
    9.     Loop While exitCode <> 0
    10.  
    11.     CloseHandle hProcess

    But it only gets the exitCode = 259, which is the corresponding one to the process still being active...

    Is this possible without having to resort to something like "echo %ERRORLEVEL% > errors.txt"?

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Sep 2006
    Posts
    160

    Re: Catching errorlevel from a batch file

    Well, notice some hours ago that I can force the exit code at any moment by doing simply "exit n", so it's just a matter of checking the ERRORLEVEL value each time to see if some error was generated.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: [RESOLVED] Catching errorlevel from a batch file

    If echo errorlev = %ERRORLEVEL% doesn't throw an error, the exit error of the batch process is 0. There's only 1 error level for a program, and that's the one it sets when it exits. You can save error levels locally (in the batch process) and force an exit level based on a previous error, even though the process has since completed a few further steps with no error.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width