WScript.Shell Exec的实时控制台输出 [英] Real time console output from WScript.Shell Exec
问题描述
我大部分时间都在寻找解决方案,我开始认为它可能无法满足我的要求
I spent most of the day searching for a solution to this, I'm starting to think its maybe not possible for my requirements
我的基本设置是运行从excel vba代码调用的vbscript(.vbs)。 vba代码必须继续运行并保持vbscript运行,但是会不时使用 Exec.Status
My basic setup is to run a vbscript (.vbs) called from an excel vba code. The vba code has to continue on and leave the vbscript running, but will monitor it from time to time using Exec.Status
在vbscript中,我使用 WScript.StdOut.WriteLine无论如何
来跟踪/调试它的进度,但是就目前的情况来说,我只能在excel vba代码完成了它需要做的事情。
In the vbscript I'm using WScript.StdOut.WriteLine "whatever"
to track/debug it's progress, but as it stands I can only read it's output after the excel vba code is finished what it needs to do.
我想要的是看到vbscript实时向控制台输出
What I want is to see a real time output to the console from the vbscript
这是vba代码...
Here's the vba code...
Dim WSH As IWshRuntimeLibrary.WshShell 'Windows Script Host Object Model
Dim Exec As WshExec
Set WSH = CreateObject("WScript.Shell")
Set Exec = WSH.Exec("%COMSPEC% /C CSCRIPT.EXE //nologo " _
& VbsFileDir _
& " " & Arg1 _
& " " & Arg2 _
& " " & Arg3 _
& " " & Arg4)
我已经能够通过将 WSH.Exec
到 WSH.Run
,但是我确实需要访问 Exec.Status
,在 WSH下不可用。运行
I have been able to get a real time output by converting from WSH.Exec
to WSH.Run
, but I do need the access to Exec.Status
, which is not available under WSH.Run
更新-2015-02-06
要进一步阐明...使用@Ekkehard提供的示例'... B.vbs'代码。Horner的答案...下面的excel-vba代码将向控制台显示实时输出...
To clarify further... Using the example '...B.vbs' code provided by @Ekkehard.Horner's answer... The following excel-vba code WILL display a real-time output to the console...
WSH.Run("cscript C:\28353522-B.vbs")
...但以下内容不会在控制台上显示任何内容
...but the following WILL NOT display anything to the console
WSH.Exec("cscript C:\28353522-B.vbs")
我不能使用 .Run()
使用 .Exec()
中的 .Status
标志同样,我不能只是将vbscript移入VBA代码,因为VBA继续与vbscript并行执行其他任务。
I can't use the .Run()
because I use the .Status
flag from .Exec()
Also I can't just move the vbscript into the VBA code because the VBA goes on to do other tasks in parallel with the vbscript.
Ps如果有人可以提交解释为什么无法完成的答案,那么我会将其标记为已接受。
P.s. If anyone can submit an answer explaining why it can't be done, then I will mark that as accepted.
推荐答案
使用。 Stdout.ReadLine()直到该过程完成,然后.Stdout.ReadAll()吞噬其余的输出-如
Use .Stdout.ReadLine() until the process has finished and .Stdout.ReadAll() to slurp the rest of the output - as in
28353522-A.vbs
28353522-A.vbs
Option Explicit
Const WshFinished = 1
Dim oExc : Set oExc = CreateObject("WScript.Shell").Exec("cscript 28353522-B.vbs")
WScript.Echo "A", "start"
Do While True
If oExc.Status = WshFinished Then
WScript.Echo "A", "WshFinished"
Exit Do
End If
WScript.Sleep 500
If Not oExc.Stdout.AtEndOfStream Then WScript.Echo "A", oExc.Stdout.ReadLine()
Loop
If Not oExc.Stdout.AtEndOfStream Then WScript.Echo "A", oExc.Stdout.ReadAll()
28353522-B.vbs
28353522-B.vbs
Option Explicit
Dim i
For i = 1 To 10
WScript.Echo "B", i, "whatever"
WScript.Sleep 100
Next
输出:
cscript 28353522-A.vbs
A start
A B 1 whatever
A B 2 whatever
A B 3 whatever
A WshFinished
A B 4 whatever
B 5 whatever
B 6 whatever
B 7 whatever
B 8 whatever
B 9 whatever
B 10 whatever
BTW-如何使用.Run获得实时输出()?
BTW - How did you get real-time output with .Run()?
这篇关于WScript.Shell Exec的实时控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!