Show output from child scripts in the parent conso

2020-07-25 17:56发布

问题:

I'm calling VBScripts from inside of a VBScript and I want their console output to appear in the window from which I'm calling them. So when I have this code

WScript.Stdout.WriteLine( "Checking out unit tests" )

ObjWshShell.Run "%comspec% \c checkoutUnitTests.vbs", 0, True

the the only output I see is

Checking out unit tests

when I want to see all the output from checkoutUnitTests.vbs concatenated onto that output in the same window. How do I do this?

回答1:

You should try to use .Exec and .Stdout.Readline() as in this bare bone demo script:

mother.vbs

Option Explicit

Dim oWS : Set oWS = CreateObject("WScript.Shell")
WScript.Echo "A", "mother starts child"
Dim oEx : Set oEx = oWS.Exec("cscript child.vbs")
Do Until oEx.Stdout.AtEndOfStream
   WScript.Echo oEx.Stdout.ReadLine()
Loop
WScript.Echo "B", "mother done"

child.vbs:

Option Explicit

Dim n
For n = 1 To 5
    WScript.Echo n, "child"
Next

output:

cscript mother.vbs
A mother starts child
1 child
2 child
3 child
4 child
5 child
B mother done

Added:

see Pythonic version