Re: How to parse output from a command
- From: "James Whitlow" <jwhitlow@xxxxxxxxxx>
- Date: Mon, 11 Jul 2005 16:15:20 -0500
See if this is more like what you are looking for:
Dim RegEx, WSH
Set RegEx = New RegExp
Set WSH = CreateObject("WScript.Shell")
Set oExec = WSH.Exec("ping.exe -n 1 localhost")
RegEx.Pattern = "^Reply from ([^:]+).*$"
Do Until oExec.Status = 0
WScript.Sleep 100
Loop
Do Until oExec.StdOut.AtEndOfStream
Line = oExec.StdOut.ReadLine
If RegEx.Test(Line) Then IP = RegEx.Replace(Line, "$1")
Loop
WScript.Echo IP
"Don" <Don@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:48C3BF9B-504B-49E6-9640-109799108833@xxxxxxxxxxxxxxxx
> 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: Don
- 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
- Re: How to parse output from a command
- From: Don
- How to parse output from a command
- Prev by Date: Re: How to parse output from a command
- Next by Date: Re: How to change colors in Msgbox or Input Box Object
- 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
|