Author Topic: IWshExec.StdErr Property  (Read 5543 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2517
  • User-Rate: +209/-0
  • Gender: Male
IWshExec.StdErr Property
« on: July 14, 2008, 08:47:59 PM »

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

JScript
Code: [Select]
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

Code: [Select]
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

Code: [Select]
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
« Last Edit: July 14, 2008, 08:49:34 PM by José Roca »

Offline Paul Breen

  • Newbie
  • *
  • Posts: 39
  • User-Rate: +1/-10
Re: IWshExec.StdErr Property
« Reply #1 on: November 23, 2010, 11:31:55 PM »
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
                       

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2517
  • User-Rate: +209/-0
  • Gender: Male
Re: IWshExec.StdErr Property
« Reply #2 on: November 24, 2010, 12:05:38 AM »
This compiles fine. Don't know what you're doing.

Code: [Select]
#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

Offline Paul Breen

  • Newbie
  • *
  • Posts: 39
  • User-Rate: +1/-10
Re: IWshExec.StdErr Property
« Reply #3 on: November 24, 2010, 12:19:39 AM »
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.