Re: Gathering output from WshScriptExec.Stdout

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Michael Harris \(MVP\) ("Michael)
Date: 01/15/05


Date: Fri, 14 Jan 2005 20:57:29 -0800

simon_c wrote:
> I'm calling a simple interactive command line program using
> WshShell.Exec().
>
> I want to be able to recieve output and send input to this program
> once it has started. The problem is that the program doesn't output
> newlines at the end of a prompt string. So it might print a string
> saying:
>
> "What is your name? "
>
> or
>
> "Enter the number of bananas: "
>
> Then the program sits there waiting for input. I want a way of
> displaying all text in the returned WshScriptExec.stdout. ReadAll()
> just hangs as there's no EOF character. ReadLine() also hangs as
> there's no newline character. Read(n) hangs if I try and read more
> than the number of chars in the buffer.
>
> I have no way of telling what a terminating char is. I just want to be
> able to view all the characters currently in the stdout buffer and
> stop reading when I run out of characters.
>
> Something like this:
>
> oWshShell = CreateObject("WScript.Shell")
> wshScript = oWshShell.Exec(f_FullCommand)
>
> Dim retstr As String
>
> Do While Not wshScript.StdOut.AtEndOfStream
> retstr = retstr & wshScript.StdOut.Read(30)
> Loop
>
> But with the obvious difference that it doesn't use AtEndOfStream. The
> FileStream class looks pretty limited so I'm guesing I can't do this.
> Perhaps ther'es another way... any ideas?

The best you can do is know in advance what prompts you will receive and
what responses they require. Then you can write them into the StdIn stream
with wshScript.StdIn.WriteLine calls before trying to read from
wshScript.StdOut (which I would suggest reading one line at a time with
ReadLine rather than ReadAll).

Of course, if the prompts and responses are unpredictable, you're pretty
much out of luck. Trying to automate a command that expects to be executed
interactively, issuing prompts and waiting for replies, can be very tricky
and sometime impossible.

-- 
Michael Harris
Microsoft MVP Scripting 


Relevant Pages

  • Gathering output from WshScriptExec.Stdout
    ... I want to be able to recieve output and send input to this program ... newlines at the end of a prompt string. ... just hangs as there's no EOF character. ...
    (microsoft.public.scripting.vbscript)
  • Re: Boot-up scripts dont output to screen?
    ... mark.bergman@xxxxxxxxxxxxx schrieb: ... prompts the user to enter a character within 10 seconds to enter a ... certain application mode (if no character is entered with 10 secs, ... (NB I think the application may have been succesfully ported to RHEL ...
    (comp.os.linux.setup)
  • Re: Left() Need the equivalent of this
    ... Whats wrong with this picture? ... You are only returning 1 character. ... Look at HELP for the MID function, or check out the prompts as you enter the ... length of the rest of the string. ...
    (microsoft.public.excel.programming)