Real'sHowTo AddThis Feed Button
Custom Search

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)

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

blog comments powered by Disqus

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2015
[ home ]