Re: How to parse output from a command



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
> > >
> >
> >
> >


.



Relevant Pages

  • Re: Shell Functions and DOS executables
    ... I had to create a .BAT file that actually made the call to the exe, ... Dim wshExec As IWshRuntimeLibrary.wshExec ... I followed the above and the DOS command executes perfectly! ...
    (microsoft.public.vb.general.discussion)
  • Re: combine two text files
    ... > that I can use to concatenate two files, ... I'd recommend using the DOS append command instead of the DOS Copy command, ... Dim sFileConcat As String ...
    (microsoft.public.access.modulesdaovba)
  • RE: Run a Stored Procedure from Excel
    ... You also want to find out if you have a rsDW variable defined. ... Your code had a semicolon between the 2nd command and the ... Dim cnnDW As ADODB.Connection ... Set cnnDW = New ADODB.Connection ...
    (microsoft.public.excel.programming)
  • RE: Run a Stored Procedure from Excel
    ... "You also want to find out if you have a rsDW variable defined. ... Your code had a semicolon between the 2nd command and the ... Dim cnnDW As ADODB.Connection ... Set cnnDW = New ADODB.Connection ...
    (microsoft.public.excel.programming)
  • RE: Open Access 2003 or 2007 from Excel
    ... 'EXAMPLE OF ACCESS COMMAND TO CONTROL FORMS ... 'Dim dbMain As ADODB.Connection ... 'Dim dbMain As ADODB.Connection 'compile error: ... new computers that now have VISTA as the OS. ...
    (microsoft.public.excel.programming)