• Welcome to Jose's Read Only Forum 2023.
 

IWshExec.StdErr Property

Started by José Roca, July 14, 2008, 08:47:59 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

José Roca



The following code demonstrates the StdErr object by attempting to execute a non-existent command and displaying the results.

JScript

var WshShell = new ActiveXObject("WScript.Shell");
var oExec    = WshShell.Exec("%comspec% /c dire");

function ReadAllFromAny(oExec)
{
     if (!oExec.StdOut.AtEndOfStream)
          return oExec.StdOut.ReadAll();
     if (!oExec.StdErr.AtEndOfStream)
          return "STDERR: " + oExec.StdErr.ReadAll();
     return -1;
}

var allInput = "";
var tryCount = 0;

while (true)
{
     var input = ReadAllFromAny(oExec);
     if (-1 == input)
     {
          if (tryCount++ > 10 && oExec.Status == 1)
               break;
          WScript.Sleep(100);
     }
     else
     {
          allInput += input;
          tryCount = 0;
     }
}

WScript.Echo(allInput);


VBScript


Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec    = WshShell.Exec("%comspec% /c dire")

Function ReadAllFromAny(oExec)
     If Not oExec.StdOut.AtEndOfStream Then
          ReadAllFromAny = oExec.StdOut.ReadAll
          Exit Function
     End If
     If Not oExec.StdErr.AtEndOfStream Then
          ReadAllFromAny = "STDERR: " + oExec.StdErr.ReadAll
          Exit Function
     End If
     ReadAllFromAny = -1
End Function

Dim allInput, tryCount
allInput = ""
tryCount = 0

Do While True
     Dim input
     input = ReadAllFromAny(oExec)
     If -1 = input Then
          If tryCount > 10 And oExec.Status = 1 Then
               Exit Do
          End If
          tryCount = tryCount + 1
          WScript.Sleep 100
     Else
          allInput = allInput & input
          tryCount = 0
     End If
Loop

WScript.Echo allInput


PowerBASIC


FUNCTION ReadAllFromAny (BYVAL pWshExec AS IWshExec) AS STRING

   LOCAL pStdOut AS ITextStream
   LOCAL pStdErr AS ITextStream
   
   pStdOut = pWshExec.StdOut
   IF ISNOTHING(pStdOut) THEN EXIT FUNCTION

   IF ISFALSE pStdOut.AtEndOfStream THEN
      FUNCTION = ACODE$(pStdOut.ReadAll)
      pStdOut = NOTHING
      EXIT FUNCTION
   END IF
   pStdOut = NOTHING

   pStdErr = pWshExec.StdErr
   IF ISNOTHING(pStdErr) THEN EXIT FUNCTION

   IF ISFALSE pStdErr.AtEndOfStream THEN
      FUNCTION = "STDERR: " & ACODE$(pStdErr.ReadAll)
      pStdErr = NOTHING
      EXIT FUNCTION
   END IF
   pStdErr = NOTHING

END FUNCTION

FUNCTION PBMAIN

   LOCAL pWsh3 AS IWshShell3
   LOCAL pWshExec AS IWshExec
   LOCAL strInput AS STRING
   LOCAL strAllInput AS STRING
   LOCAL tryCount AS LONG

   pWsh3 = NEWCOM "WScript.Shell"
   IF ISNOTHING(pWsh3) THEN EXIT FUNCTION
   pWshExec = pWsh3.Exec(UCODE$("%comspec% /c dire"))

   DO
      strInput = ReadAllFromAny(pWshExec)
      IF strInput = "" THEN
         IF tryCount > 10 AND pWshExec.Status = 1 THEN EXIT DO
         tryCount = tryCount + 1
         SLEEP 100
      ELSE
         strAllInput = strAllInput & strInput
         EXIT DO
      END IF
   LOOP

   STDOUT strAllInput

   pWshExec = NOTHING
   pWsh3 = NOTHING

   WAITKEY$

END FUNCTION


Paul Breen

Hello:
It may be late, but the powerbasic code can't work. First, it needs #INCLUDE "wshom.inc" but after I put that in something turned up wrong in the scrrun.inc file that causes this error:

"Error 543 in I:\PB\PBWIN90\WINAPI~1\SCRRUN.INC(18:001):  Must be outside Sub/Function/Class... Line 18: $PROGID_ScriptingDictionary = "Scripting.Dictionary" " I don't know how to fix this.

I am using Joses' version 117 for the include files.

thanks,
PB
                       

José Roca

This compiles fine. Don't know what you're doing.


#compile exe
#dim all
#include "wshom.inc"

FUNCTION ReadAllFromAny (BYVAL pWshExec AS IWshExec) AS STRING

   LOCAL pStdOut AS ITextStream
   LOCAL pStdErr AS ITextStream
   
   pStdOut = pWshExec.StdOut
   IF ISNOTHING(pStdOut) THEN EXIT FUNCTION

   IF ISFALSE pStdOut.AtEndOfStream THEN
      FUNCTION = ACODE$(pStdOut.ReadAll)
      pStdOut = NOTHING
      EXIT FUNCTION
   END IF
   pStdOut = NOTHING

   pStdErr = pWshExec.StdErr
   IF ISNOTHING(pStdErr) THEN EXIT FUNCTION

   IF ISFALSE pStdErr.AtEndOfStream THEN
      FUNCTION = "STDERR: " & ACODE$(pStdErr.ReadAll)
      pStdErr = NOTHING
      EXIT FUNCTION
   END IF
   pStdErr = NOTHING

END FUNCTION

FUNCTION PBMAIN

   LOCAL pWsh3 AS IWshShell3
   LOCAL pWshExec AS IWshExec
   LOCAL strInput AS STRING
   LOCAL strAllInput AS STRING
   LOCAL tryCount AS LONG

   pWsh3 = NEWCOM "WScript.Shell"
   IF ISNOTHING(pWsh3) THEN EXIT FUNCTION
   pWshExec = pWsh3.Exec(UCODE$("%comspec% /c dire"))

   DO
      strInput = ReadAllFromAny(pWshExec)
      IF strInput = "" THEN
         IF tryCount > 10 AND pWshExec.Status = 1 THEN EXIT DO
         tryCount = tryCount + 1
         SLEEP 100
      ELSE
         strAllInput = strAllInput & strInput
         EXIT DO
      END IF
   LOOP

   STDOUT strAllInput

   pWshExec = NOTHING
   pWsh3 = NOTHING

   WAITKEY$

END FUNCTION


Paul Breen

Oops.
I put the "#include wshom.inc" inside of pbmain(). Since this fixed the original problem of not finding an identifier,  I looked over it.