Share this page 

Capture the output of an external programTag(s): WSH VBScript


First technique : Redirect to a file and the read it.
Const TemporaryFolder = 2

strDOSCmd = "dir c:\*.txt /b /on"
Set WSHShell = CreateObject("Wscript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
tempfile = fso.GetTempName
path = fso.GetSpecialFolder(TemporaryFolder)
tempfile = path & "\" & tempfile
WSHShell.Run _
   "%comspec% /c " & strDOSCmd & " >" & tempfile, 0, true
arResults = Split(fso.OpenTextFile(tempfile).ReadAll,vbcrlf)
fso.DeleteFile tempfile
WScript.Echo join(arResults,vbcrlf)
WScript.Quit(0)

Second technique : grab the stdout stream and parse it.

Set objShell = CreateObject("WScript.Shell")
Set objWshScriptExec = objShell.Exec("ipconfig /all")
Set objStdOut = objWshScriptExec.StdOut

While Not objStdOut.AtEndOfStream
   strLine = objStdOut.ReadLine
   ' english windows
   If InStr(strLine,"Physical Address") Then
       WScript.Echo strLine
   ' french windows       
   ElseIf InStr(strLine,"Adresse physique") Then
       WScript.Echo strLine    
   End If
Wend