Re: How to parse output from a command
- From: Don <Don@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 11 Jul 2005 13:58:10 -0700
Thanks for the reply. I'm not actually trying to ping a computer. I'm
sorry, I should've been more specific.
What I'm trying to do is extract a word out of stdout, in this case, the IP
Address itself. I'm just using the ping scenario as an easy example. But
I'm trying to find a way to get any string out of any output. As long as I
know that some of the delimited strings will always be the same, I should be
able to retrieve a value of an unkown string in that line.
I tried this:
[code]
SET objShell=CreateObject("Wscript.Shell")
DIM objExec, strResult
SET objExec = objShell.Exec("for /f " & Chr(34) & "tokens=3 delims=: " & _
Chr(34) & " %i in ('ping -n 1 localhost ^| find " & Chr(34) & "Reply
from " & _
Chr(34) & "') do @echo %i")
WHILE objExec.Status <> WshFinished
'wait for command to complete
WEND
strResult = objExec.StdOut.ReadAll
WScript.Echo strResult
[/code]
But it says it cannot find the file specified. And I was hoping I could
bypass the DOS command all together, thinking there would be an easy way to
do this straight out of VB.
In the DOS way of doing this as per the example, I can "find" the line with
the words "Reply from", and parse that with the "for /f" statement by
locating the 3rd token, which is the IP address I'm looking for.
The same would hold true if I was trying to find the default gateway ip
address of a workstation, like:
[code]
for /f "tokens=12 delims=: " %i in ('ipconfig ^| find "Default Gateway"') do
@echo %i
[/code]
So, I'm trying to find a way to either do this by executing the DOS command
externally from the VBScript, or directly from VB if there is a way...
I don't know if that explanation helps any. I think I just confused myself
even more!
"Ed Thurber" wrote:
> Try this
>
> Function Ping_Host(Target)
>
> Dim oExec, CommandLine
>
> CommandLine = "Ping -n 1 -w 100 " & Target
>
> Set oExec = objShell.Exec(CommandLine)
>
> '## Examine output of exec command line by line (kill loop when no more
> input)
> Do Until oExec.StdOut.AtEndOfStream
> Input = oExec.StdOut.ReadLine
> ' '## If host replies, set ping_host to 1 then Exit
> if InStr(Input, "Reply") And not Instr(input, "unreachable") Then
> Ping_Host = True
> Exit Function
> End If
> Loop
> Ping_Host = FALSE
>
> End Function
>
>
> "Don" <Don@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:4EFA5A17-EB52-4DD7-ABA4-CEA1F6477505@xxxxxxxxxxxxxxxx
> >I realize this is more of a windows shell scripting questions, but this new
> > menu thing... well, it sucks quite frankly.
> >
> > Anyway, how would I get the same results out of VBScript, that I would
> > from
> > this DOS batch command?
> >
> > for /f "tokens=3 delims=: " %i in ('ping -n 1 localhost ^| find "Reply
> > from"') do @echo %i
> >
>
>
>
.
- Follow-Ups:
- Re: How to parse output from a command
- From: Torgeir Bakken \(MVP\)
- Re: How to parse output from a command
- From: TDM
- Re: How to parse output from a command
- From: James Whitlow
- Re: How to parse output from a command
- References:
- How to parse output from a command
- From: Don
- Re: How to parse output from a command
- From: Ed Thurber
- How to parse output from a command
- Prev by Date: How to delet unwanted files and folders
- Next by Date: Re: How to parse output from a command
- Previous by thread: Re: How to parse output from a command
- Next by thread: Re: How to parse output from a command
- Index(es):
Relevant Pages
|