Results 1 to 14 of 14

Thread: [RESOLVED] parsing a text file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    18

    Resolved [RESOLVED] parsing a text file

    I need to code a hitcounter button that looks at a textfile,counts how many times a site has been visited and then shows a message box with the results
    say like this:

    www.google.com 4
    www.vbforums.com 98

    anyone have any advice on how to go about it?tis the last piece of my web browser puzzle.(well almost)

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: parsing a text file

    You missed out the information as to what is in the file?

    If the file is a list of sites, one per line, then you could do something like this;

    Code:
            Dim Info() As String = System.IO.File.ReadAllLines("MyFile.txt")
    
            Dim Sites As New List(Of String)
            Dim Hits As New List(Of Integer)
    
            For Each s As String In Info
                If Not Sites.Contains(s) Then
                    Sites.Add(s)
                    Hits.Add(1)
                Else
                    Hits(Sites.IndexOf(s)) += 1
                End If
            Next
    
            Dim Str As String = String.Empty
            For i As Integer = 0 To Sites.Count - 1
                Str += Sites(i) & " has " & Hits(i).ToString & " hits" & ControlChars.NewLine
            Next
            MessageBox.Show(Str)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    18

    Re: parsing a text file

    whoops yeh i should have mentioned that.
    the text file has url and timestamp info in it seperated by commas,so its like
    Code:
    http://www.google.co.uk/ , 28/03/2009 12:19:38
    thats a sample from the history.txt file it makes.
    i gave your suggestion a try,it works but not quite as i need it too,i think i will need to alter it to take out the timestamp..also i need it to read subpages from a any domain as hits for that domain,ie
    Code:
    http://www.google.co.uk/ , 28/03/2009 12:19:38 
    http://www.google.co.uk/search=example , 28/03/2009 12:19:45
    would read as 2 hits for the www.google.com ..not sure how to go about this.
    thanks a lot for your help.
    Last edited by Andyonline2009; Mar 29th, 2009 at 05:09 AM.

  4. #4
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: parsing a text file

    I guess we met in another thread, but here's an example how to grab the domain part;

    Code:
            Dim r As New Regex( _
            "(?<=http://www.)[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)", _
            RegexOptions.IgnoreCase Or RegexOptions.Singleline)
            Dim matches As MatchCollection = r.Matches(TextBox1.Text)
            If matches.Count > 0 Then MessageBox.Show(matches(0).ToString)

  5. #5
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: parsing a text file

    So if you have lines which contain;

    http://www.google.co.uk/ , 28/03/2009 12:19:38
    http://www.google.co.uk/search=example , 28/03/2009 12:19:45

    Do you want to register one hit or two?

  6. #6
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: parsing a text file

    If you want just one hit registered then;

    Code:
            Dim Info() As String = System.IO.File.ReadAllLines("MyFile.txt")
    
            Dim Sites As New List(Of String)
            Dim Hits As New List(Of Integer)
    
            Dim matches As MatchCollection
    
            For Each s As String In Info
    
                Dim r As New Regex( _
                "(?<=http://www.)[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\s", _
                RegexOptions.IgnoreCase Or RegexOptions.Singleline)
                matches = r.Matches(s)
                If matches.Count > 0 Then
                    Dim Domain As String = matches(0).ToString
                    If Not Sites.Contains(Domain) Then
                        Sites.Add(Domain)
                        Hits.Add(1)
                    Else
                        Hits(Sites.IndexOf(Domain)) += 1
                    End If
                Else
                    'No domain was found on the line
                End If
            Next
    
            Dim Str As String = String.Empty
            For i As Integer = 0 To Sites.Count - 1
                Str += Sites(i) & " has " & Hits(i).ToString & " hits" & ControlChars.NewLine
            Next
            MessageBox.Show(Str)

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    18

    Re: parsing a text file

    well two counts..as the domain is visited twice.thanks for you help so far.im going to try your code above

  8. #8
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: parsing a text file

    Try putting the full path to your history file like C:\Documents and Settings\YourName\Desktop\history.txt or whatever.

    Also copy my code in post #6, what you posted above wont work.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    18

    Re: parsing a text file

    yeh i knew my code was wrong(it almost always is)
    im trying your suggestion now,it compiles fine but the message box it shows is blank.

  10. #10
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: parsing a text file

    Post or PM me part of your history file so I can see the exact format (just cut and paste into the message text).

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    18

    Re: parsing a text file

    http://www.google.co.uk/ , 29/03/2009 14:34:38
    http://www.google.co.uk/ , 29/03/2009 14:35:02
    http://www.google.co.uk/test , 29/03/2009 14:35:02
    http://www.bbc.co.uk/ , 29/03/2009 14:35:02

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    18

    Re: parsing a text file

    thats the contents of the history file

  13. #13
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: parsing a text file

    ok , thats because you have a "/" at the end of the domain, so to accept that modify the Regex as per the code below;

    Code:
            Dim Info() As String = System.IO.File.ReadAllLines("MyFile.txt")
    
            Dim Sites As New List(Of String)
            Dim Hits As New List(Of Integer)
    
            Dim matches As MatchCollection
    
            For Each s As String In Info
    
                Dim r As New Regex( _
                "(?<=http://www.)[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)(?=/\s)", _
                RegexOptions.IgnoreCase Or RegexOptions.Singleline)
                matches = r.Matches(s)
                If matches.Count > 0 Then
                    Dim Domain As String = matches(0).ToString
                    If Not Sites.Contains(Domain) Then
                        Sites.Add(Domain)
                        Hits.Add(1)
                    Else
                        Hits(Sites.IndexOf(Domain)) += 1
                    End If
                Else
                    'No domain was found on the line
                End If
            Next
    
            Dim Str As String = String.Empty
            For i As Integer = 0 To Sites.Count - 1
                Str += Sites(i) & " has " & Hits(i).ToString & " hits" & ControlChars.NewLine
            Next
            MessageBox.Show(Str)

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    18

    Re: parsing a text file

    working perfectly now,can't thank you enough

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width