All Forums >> [Scripting] >> WSH & Client Side VBScript >> extended Textstream and WScript.Shell -- oh dear! Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
I've done a bunch of searching and reading through this forum to try and fix a pesky bug in a set of scripts i'm writing; but haven't managed to sort it out :(
I'm building a custom class called uProcess, which extends the WScript.Shell object
The code above is a snippet from inside my 'Status' function.. in the code snipped-out it simply takes the data read and writes it to a textstream
Now everything here works fine, unless StdOut is 'empty' ... which is why there is such a strange do, loop.... i'm hopeless at explaining these things, but my problem is as follows:
* execute "cmd /c pause" [ (press any key to continue . . . ) ] * script starts processing text sent to StdOut and storing it in a seperate textstream * process is now waiting for input, and nothing is being written to StdOut * script checks to see if "AtEndOfStream" for StdOut, and hangs.
Does anyone know of a way I can check to see if m_oExec.StdOut is empty? I thought that by using an 'AtEndOfStream' test I would be able to read from the buffer until it reached the end..
again sorry for terminology, coding style etc.. i'm not a programmer.
Maybe it would be better if you explain what you are trying to accomplish. Trying to write and read to and from StdOut and StdIn, while possible is often more difficult than it needs to be. Perhaps there is a way to accomplish your goals without using them at all.
I've been building a function suite to interface with some software we use in house (development studio) more specifcally, the scripts will be integrated into 'Perforce' the sub-version control program
I have some utilities such as:
uString.vbs Object based wrapper for the existing Variant value type string. Implemented to suppor a more consistent intuitive string type.
and then:
uStringStream.vbs Which is in the same style as the uString script above, and provides full polymorphisism with true text stream objects
now we have
uProcess.vbs
Which is used to execute a command, and perform all the IO operations; but capture the data and store it in uStringStream objects.
The TextStream used to store the data is initialized as such:
Then in uProcess here is my 'Execute' method
now further into the uProcess, I have Get Status, which as can be seen in my first post, is like what i'd call a "run time loop" no idea what the real terminology is... basically it gets called repeatedly until the process throws out a 1, and in doing so processes the streams and flushes them into the textstream objects I mentioned earlier.
I've cut down my status property alot so it's only dealing with output right now.
so now to actually use these functions, in a testing script:
Obviously, Status will never reach 1, as the process is waiting for a key input (press any key to continue) but that's not the problem, the problem is that it will read "Press any key to continue . . . " from the StdOut stream, then hang there at the "AtEndOfStream" test.
I'm not good at explaining this but i've tried my best, and i've rtfm, and quite possibly every piece of documentation i can find on vbscript. and i'm lost.
It may take 4 or 5 goes, but I can try and explain more if this is still really ambiguous.
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
It looks quite a lot as your script part, but there are minor differences. You can judge for yourself if this gives a solution.
Yeah, I got the idea to try using 'Pause' from that sample on MSDN.. it's a good way to check if input and output are able to work in synchronization
If you notice, i'm not doing anything different in my script as they are in theirs; therefore if their code is used to read the full line of console output it will also crash.
this is how they're preventing their script from crashing, you see it's exiting the DO loop when it finds "Press any key" ; StdOut will still have " to continue . . . " in it.
If you took that test out, it will crash; but what if you wanted to capture all of it?
I can't understand what the use of StdOut,In,Err are if i'm unable to read correctly from them.
At the very most basic forms of what I need to do is after launching a process, I need to be able to capture everything sent to StdOut and store it in a TextStream
My code will do that fine, for example if you change it to exec "cmd /c dir %windir%" then it will read all that in and store it in the textstream.
But in a situation, such as when the process is waiting for input, absolutely ANY functions performed on StdOut cause the code to hang, like StdOut is null or something
So that's the end? :( ah no, what am I going to do..
I totally shouldn't have left this one till last ;)