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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Execute external files from vbs 1

Status
Not open for further replies.

kridy

Programmer
May 21, 2001
12
DE
Hi there,

is it possible to execute an external file and get its output back, like it can be done in Perl using

@output = `C:\\test.exe`;

?

I am looking forward to getting some help ;-)
Kind regards,
Kridy
_____________
kridy@web.de
 
kridy,

This is the old way of doing things.

Set Wsh = Wscript.CreateObject("Wscript.Shell")
Wsh.run ("cmd /K ""dir c:\somedir\*.* > c:\dev\scripts\somedir.txt"" ")

However the newest Windows Script Host 5.6 from Microsoft allows you to capture output and re-direct it. I can't remember where I got this but it works well.

fengshui_1998

' Save this to a .vbs file, remember you must have 5.6.
' ------------------------------------------------------
Set oExec = wshShell.exec("cmd /C Dir c:\directoryname")

Function ReadAllFromAny(oExec)

If Not oExec.StdOut.AtEndOfStream Then
ReadAllFromAny = oExec.StdOut.ReadAll
Exit Function
End If

If Not oExec.StdErr.AtEndOfStream Then
ReadAllFromAny = "STDERR: " + oExec.StdErr.ReadAll
Exit Function
End If

ReadAllFromAny = -1
End Function

Dim allInput, tryCount

allInput = ""
tryCount = 0

Do While True

Dim input
input = ReadAllFromAny(oExec)

If -1 = input Then
If tryCount > 10 And oExec.Status = 1 Then
Exit Do
End If
tryCount = tryCount + 1
WScript.Sleep 100
Else
allInput = allInput & input
tryCount = 0
End If
Loop

WScript.Echo allInput


 
Hi fengshui_1998!

Many thanks for Your help! I have tried the new way and it works :)

Kind regards,
Kridy
_____________
kridy@web.de
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top