Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WScript.Shell.Exec - read output from stdout

Tags:

vbscript

My VBScript does not show the results of any command I execute. I know the command gets executed but I would like to capture the result.

I have tested many ways of doing this, for example the following:

Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"

Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)

Select Case WshShellExec.Status
   Case WshFinished
       strOutput = WshShellExec.StdOut.ReadAll
   Case WshFailed
       strOutput = WshShellExec.StdErr.ReadAll
 End Select

WScript.StdOut.Write strOutput  'write results to the command line
WScript.Echo strOutput          'write results to default output

But it does not print any results. How do I capture StdOut and StdErr?

like image 852
Daniel Marín Avatar asked Mar 25 '26 12:03

Daniel Marín


2 Answers

WScript.Shell.Exec() returns immediately, even though the process it starts does not. If you try to read Status or StdOut right away, there won't be anything there.

The MSDN documentation suggests using the following loop:

Do While oExec.Status = 0
     WScript.Sleep 100
Loop

This checks Status every 100ms until it changes. Essentially, you have to wait until the process completes, then you can read the output.

With a few small changes to your code, it works fine:

Const WshRunning = 0
Const WshFinished = 1
Const WshFailed = 2
strCommand = "ping.exe 127.0.0.1"

Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)

Do While WshShellExec.Status = WshRunning
     WScript.Sleep 100
Loop

Select Case WshShellExec.Status
   Case WshFinished
       strOutput = WshShellExec.StdOut.ReadAll()
   Case WshFailed
       strOutput = WshShellExec.StdErr.ReadAll()
 End Select

WScript.StdOut.Write(strOutput)  'write results to the command line
WScript.Echo(strOutput)          'write results to default output
like image 156
Nate Barbettini Avatar answered Mar 27 '26 21:03

Nate Barbettini


You should read both streams INSIDE the loop as well as after it. When your process is verbose then it will block on the I/O buffer when this buffer will not be emptyfied succesively!!!

In practice I use this schema in many scripts. It collects both streams: the output (as Shell.Exec called apps can return output info) and error.

        Dim vErrStr
        vErrStr = ""
        Dim vOutStr
        vOutStr = ""
        Do While oExec.Status = 0
            WScript.Sleep 1000
            If Not oExec.StdErr.AtEndOfStream Then
                vErrStr = vErrStr & oExec.StdErr.ReadAll
            End If
            If Not oExec.StdOut.AtEndOfStream Then
                vOutStr = vOutStr & oExec.StdOut.ReadAll
            End If
        Loop

        vExitCode = oExec.ExitCode
        If Not oExec.StdErr.AtEndOfStream Then
            vErrStr = vErrStr & oExec.StdErr.ReadAll
        End If
        If Not oExec.StdOut.AtEndOfStream Then
            vOutStr = vOutStr & oExec.StdOut.ReadAll
        End If

        Dim vEMStr
        vEMStr = vErrStr & IIf((vErrStr <> "") And (vOutStr <> ""), " | ", "") & vOutStr
like image 45
Tomek Avatar answered Mar 27 '26 20:03

Tomek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!