Re: How to parse output from a command

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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: DOS commands go bye-bye
    ... Ping is not a DOS command or program; ... - something else overrides Ping.exe and that runs a missing file ...
    (microsoft.public.windowsxp.general)
  • Re: JCL Problem...
    ... I have a utility that outputs a string to SYSPRINT (but that can be ... //* NOW I NEED TO EXECUTE A COMMAND THAT CONTAINS ... PING the.ip.addr.here ... My first idea was to pass the SYSPRINT from STEP01 to a following STEP ...
    (bit.listserv.ibm-main)
  • TIP #185: Null Handling
    ... nulls, and command modifications for manipulating them. ... Tcl deals with strings, the universal medium for representing data. ... is know and it is an empty string, but if a respondent forgets to give ...
    (comp.lang.tcl)
  • Re: Determine how Word was launched
    ... inspect the command line used to launch Word. ... Private Declare Function GetCommandLineA Lib "kernel32" As Long ... Public Function GetCommandLineAs String ... Dim lngCmdLinePtr As Long ...
    (microsoft.public.office.developer.vba)
  • Re: Class.getMethod in classs static initializer block
    ... I just wanted to add that I had some similar code for a "command line server" project. ... static private final boolean DEBUG = true; ... {public ReturnCode runCommand(String s) ... public ReturnCode parseLine(String line) ...
    (comp.lang.java.programmer)