Author Topic: IWshExec.StdOut Property  (Read 2769 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.StdOut Property
« on: July 14, 2008, 08:41:10 PM »

The following code starts a batch file and waits for the user input prompt. After entering the needed data through the StdIn stream, the batch file will be able to complete.

JScript
Code: [Select]
var WshShell = new ActiveXObject("WScript.Shell");
var oExec    = WshShell.Exec("test.bat");
var input = "";

while (true)
{
     if (!oExec.StdOut.AtEndOfStream)
     {
          input += oExec.StdOut.Read(1);
          if (input.indexOf("Press any key") != -1)
               break;
     }
     WScript.Sleep(100);
}

oExec.StdIn.Write("\n");

while (oExec.Status != 1)
     WScript.Sleep(100);

VBScript

Code: [Select]
Dim WshShell, oExec, input
Set WshShell = CreateObject("WScript.Shell")
Set oExec    = WshShell.Exec("test.bat")
input = ""

Do While True
     If Not oExec.StdOut.AtEndOfStream Then
          input = input & oExec.StdOut.Read(1)
          If InStr(input, "Press any key") <> 0 Then Exit Do
     End If
     WScript.Sleep 100
Loop

oExec.StdIn.Write VbCrLf

Do While oExec.Status <> 1
     WScript.Sleep 100
Loop

PowerBASIC
Code: [Select]
LOCAL pWsh3 AS IWshShell3
LOCAL pWshExec AS IWshExec
LOCAL pStdIn AS ITextStream
LOCAL pStdOut AS ITextStream
LOCAL strInput AS STRING

pWsh3 = NEWCOM "WScript.Shell"
IF ISNOTHING(pWsh3) THEN EXIT FUNCTION
pWshExec = pWsh3.Exec(UCODE$("test.bat"))

pStdOut = pWshExec.StdOut
IF ISNOTHING(pStdOut) THEN EXIT FUNCTION

DO
   IF pStdOut.AtEndOfStream THEN EXIT DO
   strInput = strInput & ACODE$(pStdOut.Read(1))
   IF INSTR(strInput, "Press any key") <> 0 THEN EXIT DO
   SLEEP 100
LOOP
pStdOut = NOTHING

pStdIn = pWshExec.StdIn
pStdIn.Write UCODE$($CRLF)
pStdIn = NOTHING

DO
   IF pWshExec.Status = 1 THEN EXIT DO
   SLEEP 100
LOOP

pWshExec = NOTHING
pWsh3 = NOTHING

PRINT "Done."
WAITKEY$

test.bat

Code: [Select]
@echo off
echo Press any key
pause
« Last Edit: July 14, 2008, 08:42:43 PM by José Roca »