|
-
Mar 28th, 2009, 08:23 AM
#1
Thread Starter
Junior Member
[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)
-
Mar 28th, 2009, 02:47 PM
#2
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)
-
Mar 29th, 2009, 04:55 AM
#3
Thread Starter
Junior Member
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.
-
Mar 29th, 2009, 09:05 AM
#4
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)
-
Mar 29th, 2009, 09:39 AM
#5
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?
-
Mar 29th, 2009, 09:46 AM
#6
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)
-
Mar 29th, 2009, 09:51 AM
#7
Thread Starter
Junior Member
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
-
Mar 29th, 2009, 09:54 AM
#8
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.
-
Mar 29th, 2009, 10:01 AM
#9
Thread Starter
Junior Member
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.
-
Mar 29th, 2009, 10:03 AM
#10
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).
-
Mar 29th, 2009, 10:08 AM
#11
Thread Starter
Junior Member
-
Mar 29th, 2009, 10:08 AM
#12
Thread Starter
Junior Member
Re: parsing a text file
thats the contents of the history file
-
Mar 29th, 2009, 10:13 AM
#13
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)
-
Mar 29th, 2009, 10:15 AM
#14
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|