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!

Capture Data from a command window 2

Status
Not open for further replies.

cornboy88

Programmer
Jan 16, 2002
59
US
Does anybody know how to capture data from a command window? I am sending the ping command and want to retrieve the data from the command window and write it to a file.

sub pingit

Dim VarX
Dim msg


txtName = Inputbox("Enter the IP our workstation name:")


If txtName <> &quot;&quot; Then
set wshShell = WScript.CreateObject(&quot;WScript.shell&quot;)
wshShell.run(&quot;command.com /k ping &quot; & txtName)
Else

VarX = MsgBox(&quot;You have not entered some bad information.&quot;, vbCritical, &quot;Error has occured&quot;)
End If

end sub
 
cornboy88,

Try this,


Dim WshShell, oExec
Set WshShell = CreateObject(&quot;WScript.Shell&quot;)
Dim allInput, tryCount

Set oExec = wshShell.exec(&quot;cmd /C &quot; & &quot;Dir c:\mydir&quot;)
allInput = &quot;&quot;
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


Function ReadAllFromAny(oExec)

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

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

ReadAllFromAny = -1
End Function


fengshui_1998
 
fengshui_1998,

That is just what i needed. Thanks


cornboy88
 
FWIW, in this case, it'd be easier to let the command emulator do the grunt work, ISO polling to monitor the output stream.

wshShell.run(&quot;command.com /k ping &quot; & txtName &quot; >C:\ping.txt&quot;,0,True) Jon Hawkins
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top