VBscript code to capture stdout, without showing console window

batch-filestdoutvbscript

This is a VBScript code example that shows how to catch whatever a command line program sends to standard output.
It executes the command xcopy /? and shows the output in a message box. Before the message box appears, for a split second you see the console window popping up.

Set objShell = WScript.CreateObject("WScript.Shell")
Set objExec = objShell.Exec("xcopy /?")
Do
    line = objExec.StdOut.ReadLine()
    s = s & line & vbcrlf
Loop While Not objExec.Stdout.atEndOfStream
WScript.Echo s

Here is an other VBScript code example that shows how to execute a script without showing the console window.

objShell.Run "c:\temp\mybatch.bat C:\WINDOWS\system32\cmd.exe", 0

or

objShell.Run "c:\temp\myscript.vbs C:\WINDOWS\system32\cscript.exe", 0

As you can see it has the form <script><space><executor>.
The last example uses objShell.Run instead of objShell.Exec

What I don't know is how to execute a command line program (if necessary from a batch file), catch the standard output, without showing the console window. Any ideas?

Best Solution

I usually use this:

Wscript.echo execStdOut("ping google.com")

Function execStdOut(cmd)
   Dim goWSH : Set goWSH = CreateObject( "WScript.Shell" ) 
   Dim aRet: Set aRet = goWSH.exec(cmd)
   execStdOut = aRet.StdOut.ReadAll()
End Function 

For more advanced commands youc an wrap to comspec (cmd)

my res = execStdOut("%comspec%" & " /c " & """" & "dir /b c:\windows\*.exe" & """" & " && Echo. && Echo finished") 
Related Question