0

I have a batch file that gets some parameters from the command line and returns some values into STDOUT.

I want the batch file to be "silent" (such that the console is not shown), and found out probably the only possibility of using vbs script.

I used this thread for implementing the argument forwarding to the batch file in VBS.

Then, I used the following command for calling the batch file I wrapped:

CreateObject("WScript.Shell").Run batchFilePath & " " & Trim(arglist), 0, False

It turns out that my batch file does run, but its STDOUT is discarded somewhere, and does not make its way back to whom called the VBS script. I.e. the batch file's STDOUT is not redirected into the VBS script's STDOUT.

How can I make the batch file's STDOUT being redirected to the VBS script STDOUT, such that if I start the VBS script from some shell, the batch file's output will be printed to the shell too?

SomethingSomething
  • 11,491
  • 17
  • 68
  • 126

2 Answers2

1

Use Exec instead of Run, like this:

set objShell = CreateObject( "WScript.Shell" )
cmd = "echo Hello World!"
' Run the process
set objRes = objShell.Exec( "cmd /c """ & cmd & """" )
' Wait for the child process to finish
Do While objRes.Status = 0
     WScript.Sleep 100
Loop
' Show whatever it printed to its standard output
Wscript.Echo "The output was:" & vbNewLine & objRes.StdOut.ReadAll()
Soonts
  • 20,079
  • 9
  • 57
  • 130
0

Try this...

Intreturn = WshShell.Run("cmd /c " & path& " " & args & ">c:\batchoutput.txt", 0, true) 
Set fso = CreateObject("Scripting.FileSystemObject") 
Set objfile = fso.OpenTextFile("c:\batchoutput.txt", 1)
text = objfile.ReadAll 
Objfile.Close

Or try this...

Set WshShell = WScript.CreateObject("WScript.Shell") 

Set objexc = WshShell.Exec("cmd /c " & command and args) 'replace command and args with proper variables
strOutputText = ""
While Not objexc.StdOut.AtEndOfStream
     strOutputText = strOutputText & objexc.StdOut.ReadLine()
Loop

Msgbox strOutputText

You may need some debugging on this.

Mithilesh Indurkar
  • 481
  • 1
  • 5
  • 12
  • The only way to redirect a child process' output into your self's STDOUT is by redirecting it into a file and then reading it and printing it? – SomethingSomething Jul 02 '17 at 09:51