Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to parse specific output in vb.net

Posted on 2009-07-13
7
Medium Priority
?
201 Views
Last Modified: 2012-05-07
I have the code working with the exception of it parsing only the output that I require. I'm having problems getting that to work. I'm wanting only the output to be the ip address within the brackets [xxx.xxx.xxx.xxx]


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If IO.File.Exists("C:\monitorlog.txt") Then
            IO.File.Delete("C:\monitorlog.txt")
        End If
 
        Dim Writer As New IO.StreamWriter("C:\monitor.txt", True)
        Dim item As Object
        For Each item In ListBox1.Items
            Dim machine As String = item.ToString
 
            ProgressBar1.Maximum = ListBox1.Items.Count
            ProgressBar1.Minimum = 0
            If ProgressBar1.Value < ListBox1.Items.Count Then
                ProgressBar1.Value = ProgressBar1.Value + 1
            End If
        Next
        'Try
        Dim si As New System.Diagnostics.ProcessStartInfo("cmd.exe")
 
        'Dim p As Process
        Dim x As String
        'Dim item
        Dim counter As Integer = 100            'you may need to play with this number to get it right for your system
        Dim y(counter) As Char
        Dim i As Integer
        Try
            Dim p As Process = New Process
            Dim entirefile
            Dim oread
 
            p.StartInfo.Arguments = " " & item
            'PrintDriverInfo.exe /S:MyServerName
            p.StartInfo.RedirectStandardOutput = True
            p.StartInfo.RedirectStandardInput = True
 
            p.StartInfo.CreateNoWindow = True
            p.StartInfo.UseShellExecute = False
            p.StartInfo.FileName = "ping.exe"
            p.Start()
 
            Dim sr As IO.StreamReader = p.StandardOutput 'out is a streamreader
            Dim sw As IO.StreamWriter = p.StandardInput  'inn is a streamwriter
            For i = 0 To counter - 1
 
            Next
            While Not p.HasExited
            End While
            Dim s As String = sr.ReadToEnd()
            If s <> "" Then
 
            End If
 
 
 
            If Not s Is Nothing Then
                          
 
 
                If s.StartsWith("Pinging") Then
                    Dim ip As String = s
 
                    If s.StartsWith("Pinging") Then
 
                        Dim u As Integer = s.IndexOf("[")
                        Dim r As Integer = s.IndexOf("]")
                        Dim q As Integer = s.IndexOf("]") - s.IndexOf("[")
                        s = s.Remove(u - r, s.Length - q)
                        WriteLog(s)
 
 
 
 
                        WriteLog(s)
 
 
 
 
                        p.Close()
 
 
                        
                    End If
 
 
                End If
 
            End If
 
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
 
 
 
 
 
 
 
 
 
 
 
        ProgressBar1.Value = 0
        Writer.Close()
        'Start("C:\monitor.txt")
 
 
 
    End Sub

Open in new window

0
Comment
Question by:pover
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 24841607
I would recommend using a regular expression to find the match of xxx.xxx.xxx.

Expression: "\d{1,3}\.\d{1,3}\.\d{1,3}"
0
 

Author Comment

by:pover
ID: 24841946
Are you suggesting I put
If s.StartsWith("Pinging")


            Dim ip As String = [String].Format("\d{1,3}\.\d{1,3}\.\d{1,3}")

?
0
 
LVL 28

Expert Comment

by:strickdd
ID: 24842135
I'm not sure what the VB.Net code is exactly (a little rusty) but here is the C# of what I suggest.
using System.Text.RegularExpressions
 
private void FindMatches(string StringToFindMatches)
{
   Regex myRegEx = new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}");
 
   MatchCollection matches = myRegEx.FindMatches(StringToFindMatches);
 
   //matches will now contain a list of all IP addresses found in the file.
}

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:pover
ID: 24843543
I changed the code to reflect to this but doesn't parse the data

 If Not s Is Nothing Then
         
                ' If s.StartsWith("Pinging") Then
                If True Then

                    Dim ip As String = s

                    Dim myRegEx As New Regex("\d{1,3}\.\d{1,3}\.\d{1,3}")

                    Dim matches As MatchCollection = myRegEx.Matches(ip)

                   
                    WriteLog(s)

It doesn't parse anything I checked with msgbox(s) to be sure.

I need it some way to parse the ip address which is in the brackets on a return ping.
Like you would see in a cmd shell  pinging hostname hostname.whatever.whatever.com [xxx.xxx.xxx.xx]  and have them appear in the Richtexbox that I have on the form.
Currently not just the ip in the brackets but everything else appears in the Richtextbox.

0
 
LVL 28

Expert Comment

by:strickdd
ID: 24848340
did you check the matches object? It should contain the list of IP addresses that you want. If it is empty, then we need to refine the regex.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24848346
The matches would be in matches collection so matches(0) would contain the first match and so on.
0
 

Accepted Solution

by:
pover earned 0 total points
ID: 24849768
Unfortunately, I couldn't get it to work with those suggestions. However, this other code I was
tipped with works.

Dim s As String = sr.ReadLine()
                Do Until s Is Nothing
                    s = sr.ReadLine
                    Try
                        If s.StartsWith("Pinging") Then
                            Dim m As String() = s.Split("[")
                            Dim openbracket As String = m(1)
                            Dim splitlastbracket As String() = openbracket.Split("]")

                            Dim finalIP As String = splitlastbracket(0)

                            WriteLog(finalIP)

                        End If

                    Catch ex As Exception
                        'other error
                    End Try
                Loop
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        Next
        ProgressBar1.Value = 0
        Writer.Close()
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question