Re: Gathering output from WshScriptExec.Stdout
From: Michael Harris \(MVP\) ("Michael)
Date: 01/15/05
- Next message: Al Dunbar [MS-MVP]: "Re: More than one vbs file"
- Previous message: script fan: "Re: How to use Text-to-Speech in vbscript"
- In reply to: simon_c: "Gathering output from WshScriptExec.Stdout"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Al Dunbar [MS-MVP]: "Re: More than one vbs file"
- Previous message: script fan: "Re: How to use Text-to-Speech in vbscript"
- In reply to: simon_c: "Gathering output from WshScriptExec.Stdout"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|