Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirecting Standard Output and Hiding Command Prompt 1

Status
Not open for further replies.

Norm11

Programmer
Jul 30, 2007
12
US
Hello!

I've been working on this problem for days. I've Googled high and low for a solution, but no dice.

Here's what I want to do. Through my VBScript I want to execute an external process (program). I want to redirect the standard output of that external process to a string variable. I know how to do this with a process.exec, but the problem is process.exec shows the command prompt window. I want to hide the command prompt window.

Now I know that process.run can hide the command prompt, but to my knowledge there is no way to redirect the process's standard output DIRECTLY to a string variable, which is what I need to do.

I canNOT run the process, redirect the output to a file, and then parse the file. That is, I know how to do that, but that's not an acceptable solution for this project.

Any ideas?
Norm
 
Ok, are you running this via wscript or cscript?

I found some suggestions and modified it a bit and it worked for me. Try putting this at the top of your code:

Code:
Set oShell = WScript.CreateObject("WScript.Shell")

If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
   oShell.Run "cscript """ & WScript.ScriptFullName & """", 0, False
   WScript.Quit
End If
 
Thanks for the suggestion. I've already tried this trick, but it doesn't work. It seems to halt standard output all together--not just suppress the command prompt window.

My script without your code works perfectly except for the command prompt window showing. When I add this to the script, it stops working.
 
Hmmm, thats interesting. Mine works fine.

Here is exactly what I tested:

Code:
Set objShell = WScript.CreateObject("WScript.Shell")
Set oShell = WScript.CreateObject("WScript.Shell")

If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
   oShell.Run "cscript """ & WScript.ScriptFullName & """", 0, False
   WScript.Quit
End If 

Set objExecObject = objShell.Exec("cmd /c ping -n 3 -w 1000 192.168.1.2")
Do While Not objExecObject.StdOut.AtEndOfStream
    strText = objExecObject.StdOut.ReadLine()
    If Instr(strText, "Reply") > 0 Then
        Wscript.Echo "Reply received."
        Exit Do
    End If
Loop
 
Yes, this is very interesting. I copied and pasted your code into a new .vbs and ran it. The same thing happened--it doesn't work.

I wonder if there's a difference in the scripting engine or something...
 
With the above code, if I just double-click it, which would use wscript, it just pops up the output in message boxes with no command prompt visible.
 
If I comment out the four lines like below, the script runs just fine. With them uncommented, it does not run.

Code:
Set objShell = WScript.CreateObject("WScript.Shell")
Set oShell = WScript.CreateObject("WScript.Shell")

'If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
'   oShell.Run "cscript """ & WScript.ScriptFullName & """", 0, False
'   WScript.Quit
'End If 

Set objExecObject = objShell.Exec("cmd /c ping -n 3 -w 1000 192.168.1.2")
Do While Not objExecObject.StdOut.AtEndOfStream
    strText = objExecObject.StdOut.ReadLine()
    If Instr(strText, "Reply") > 0 Then
        Wscript.Echo "Reply received."
        Exit Do
    End If
Loop
 
Just try this for giggles:

From a command prompt, try running the script like this:


1)wscript scriptname.vbs

2)cscript scriptname.vbs


See if you get any different results.
 
Yeah...I've tried that. Interestingly, I do get different results. When I do it with cscript, it runs. With wscript, it does not. With cscript the message is displayed in the command prompt window, though.

I'm changing my file association right now to cscript to see what it does when I double click.
 
My wsh version/build is v5.6 Build 8820. My vbscript version is also 5.6.
 
Well, sure enough, calling it with cscript.exe rather than wscript.exe fixes the problem. Using cscript.exe invokes a command window when the script starts, but subsequent windows do NOT appear, which was my number 1 concern.

So thank you very much for your help. You solved my problem. One star for you coming right up.

Norm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top