Forum Discussion

subbu_valliappa's avatar
subbu_valliappa
Contributor
9 years ago

Reading batch file ERRORLEVEL into TestComplete

We are using TestComplete to run a batch file and would like to do different actions depending on the outcome of the script in the batch file. I can get %ERRORLEVEL% from the batch file (0, 1, 2, etc.) but how can I read in this into TestComplete?

 

Ideally, I would prefer to do something like this:

 

If %ERRORLEVEL% = 0

    Do Condition A

Else If %ERRORLEVEL% = 1

    Do Condition B

1 Reply

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Based on http://support.smartbear.com/viewarticle/8963/:

    '-----------------------------------------------------------------------------

    ' Function starts program and waits until it finishes
    Function ExecAndWaitForEnd(ByVal strProg, ByVal strArgs)
        Dim WshShell
        Dim oExec
        Dim strCommand
        Dim allOutput

        ExecAndWaitForEnd = -1

    '    strProg = Utilities.AnsiQuotedStr(Trim(strProg), """")
        strProg = Trim(strProg)
        If (Left(strProg, 1) <> """") Then
            strProg = """" & strProg & """"
        End If

        strCommand = strProg & " " & strArgs
        Set WshShell = CreateObject("WScript.Shell")
        Set oExec = WshShell.Exec(strCommand)

        Do While oExec.Status = 0
            BuiltIn.Delay 500
        Loop

        allOutput = ReadAllFromAny(oExec)
        Log.Message "ExecAndWaitForEnd(): Call exit code=" & oExec.ExitCode, _
                "CmdLine: " & strCommand & vbCrLf & vbCrLf & "Output: " & allOutput

        ExecAndWaitForEnd = oExec.ExitCode

        Set oExec = Nothing
        Set WshShell = Nothing
    End Function
    '-----------------------------------------------------------------------------

    Function ReadAllFromAny(ByRef oExec)

         If Not oExec.StdOut.AtEndOfStream Then
              ReadAllFromAny = oExec.StdOut.ReadAll
              Exit Function
         End If

         If Not oExec.StdErr.AtEndOfStream Then
              ReadAllFromAny = cLOGSTRERROR & "STDERR: " & oExec.StdErr.ReadAll
              Exit Function
         End If

         ReadAllFromAny = -1
    End Function
    '-----------------------------------------------------------------------------