-
Dec 27th, 2013, 02:23 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Read last 200 lines of a text file
I'm trying to read the last 200 lines of a text file - however the following returns a single line (line number 200 from the end of the file) instead of the last 200 lines
Code:
Dim lines As String() = IO.File.ReadAllLines(Strings.LogFile)
logresults = lines.Length - 200
How do i read the last 200 lines of the file?
-
Dec 27th, 2013, 02:30 PM
#2
Re: Read last 200 lines of a text file
try this:
Code:
Dim lines As String() = IO.File.ReadAllLines(Strings.LogFile)
If lines.Length >= 200 Then
lines = lines.Skip(lines.Length - 200).ToArray
End If
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 27th, 2013, 03:03 PM
#3
Re: Read last 200 lines of a text file
.paul beat me to it and that solution looks more elegant but I put this together. It needs tested a little more but since I spent the time doing it
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lines As String() = IO.File.ReadAllLines("C:\TestIt.txt")
Dim iCount = lines.Length
Dim iStart As Integer
iStart = iCount - 199
Dim strTextLine As String
For i = iStart To iCount - 1
strTextLine = System.IO.File.ReadAllLines("C:\TestIt.txt")(i)
Next
End Sub
Please remember next time...elections matter!
-
Dec 27th, 2013, 05:11 PM
#4
Thread Starter
Addicted Member
Re: Read last 200 lines of a text file
Thanks guys, unfortunately i don't appear to be having much luck with either of these solutions
Paul:i get an error using the following code:
skip is not a member of system.array
Code:
' Read last 200 lines of log file
Public Function ReadLog() As String
Dim lines As String() = IO.File.ReadAllLines(Strings.LogFile)
If lines.Length >= 200 Then
lines = lines.Skip(lines.Length - 200).ToArray
End If
Return lines.ToString
End Function
Tyson: your code has an error
So i presume it should be an integer and added it as below, but it only returns the line before last - not the whole last 200 lines
Code:
' Read last 200 lines of log file
Public Function ReadLog() As String
Dim lines As String() = IO.File.ReadAllLines(Strings.LogFile)
Dim iCount = lines.Length
Dim i As Integer
Dim iStart As Integer
iStart = iCount - 199
Dim strTextLine As String
For i = iStart To iCount - 1
strTextLine = System.IO.File.ReadAllLines(Strings.LogFile)(i)
Next
Return strTextLine
End Function
-
Dec 27th, 2013, 05:13 PM
#5
Re: Read last 200 lines of a text file
which version of vb.net are you using?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 27th, 2013, 05:15 PM
#6
Re: Read last 200 lines of a text file
which version of vb.net are you using?
it looks like you using vb2008. you need to be targeting 3.5 framework for LINQ
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 27th, 2013, 05:17 PM
#7
Thread Starter
Addicted Member
Re: Read last 200 lines of a text file
VS 2010 - however its compiled for .net 2.0 to maximize compatibility
-
Dec 27th, 2013, 05:57 PM
#8
Re: Read last 200 lines of a text file
ok. for .Net 2.0 compatibility:
Code:
Private Function getLast200Elements(Of T)(ByVal a() As T) As T()
If a.Length <= 200 Then Return a
Dim newArray(199) As T
Array.Copy(a, a.Length - 200, newArray, 0, 200)
Return newArray
End Function
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 27th, 2013, 06:06 PM
#9
Re: Read last 200 lines of a text file
VS 2010 - however its compiled for .net 2.0 to maximize compatibility
I really doubt that there are many (if any) potential users of your program who do not already have or cannot install at least 3.5 but if you insist all yopu need to do to Tyson's solution is add a declaration for i (Dim i As Integer).
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Dec 27th, 2013, 06:12 PM
#10
Re: Read last 200 lines of a text file
Originally Posted by dunfiddlin
I really doubt that there are many (if any) potential users of your program who do not already have or cannot install at least 3.5 but if you insist all yopu need to do to Tyson's solution is add a declaration for i (Dim i As Integer).
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lines As String() = IO.File.ReadAllLines("C:\TestIt.txt")
Dim iCount = lines.Length
Dim iStart As Integer
iStart = iCount - 199
Dim strTextLine As String
For i = iStart To iCount - 1
strTextLine = System.IO.File.ReadAllLines("C:\TestIt.txt")(i)
Next
End Sub
really??? start at the wrong index, reread the file for every line, + not store those lines in an array??? I forgot you were such a great programmer
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 27th, 2013, 06:28 PM
#11
Re: Read last 200 lines of a text file
Hey, I program gooder than most. I just don't read properer than, well, pretty much anybody!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Dec 27th, 2013, 06:57 PM
#12
Thread Starter
Addicted Member
Re: Read last 200 lines of a text file
Originally Posted by .paul.
ok. for .Net 2.0 compatibility:
Code:
Private Function getLast200Elements(Of T)(ByVal a() As T) As T()
If a.Length <= 200 Then Return a
Dim newArray(199) As T
Array.Copy(a, a.Length - 200, newArray, 0, 200)
Return newArray
End Function
Where would i put the location of the text file in this code?
i.e. c:\logfile.txt
I tried
Code:
Private Function getLast200Elements(Of T)(ByVal a("c:\logfile.txt") As T) As T()
however this causes an error
array bounds cannot appear in type specifiers
-
Dec 27th, 2013, 07:28 PM
#13
Re: Read last 200 lines of a text file
Dim lines() as String = IO.File.ReadAllLines("C:\logfile.txt")
Dim newLines()as string = getLast200Elements(lines)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 27th, 2013, 07:29 PM
#14
Re: Read last 200 lines of a text file
It's a Function. You pass it a value and get a result back. Are you in the habit of adding extraneous details to Function definitions?
Dim lines As String() = IO.File.ReadAllLines(Strings.LogFile)
Dim lines200 = getLast200Elements(lines)
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Dec 27th, 2013, 07:42 PM
#15
Thread Starter
Addicted Member
Re: Read last 200 lines of a text file
Sorry, i'm getting lost
Code:
' Read last 200 lines of log file
Public Function ReadLog() As String
Dim lines As String() = IO.File.ReadAllLines(Strings.LogFile)
Dim lines200 = getLast200Elements(lines)
Return lines200.ToString
End Function
Private Function getLast200Elements(Of T)(ByVal a() As T) As T()
If a.Length <= 200 Then Return a
Dim newArray(199) As T
Array.Copy(a, a.Length - 200, newArray, 0, 200)
Return newArray
End Function
-
Dec 27th, 2013, 07:48 PM
#16
Re: Read last 200 lines of a text file
how do you want the output? as an array of strings, or as a single string?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 27th, 2013, 07:52 PM
#17
Thread Starter
Addicted Member
Re: Read last 200 lines of a text file
A single string i presume, i simply want to read the last 200 lines and return them using a single return command - e.g. return logfile
-
Dec 27th, 2013, 07:57 PM
#18
Thread Starter
Addicted Member
Re: Read last 200 lines of a text file
When i return the whole logfile using a single return command it comes out such as:
27/12/2013 17:51:26Message: Application Started
27/12/2013 17:51:27 Message: Network Check Started
27/12/2013 17:51:51
Error In:
Line Number:0
Error Message: Unable to connect to the remote server
27/12/2013 17:51:51 Message: Internet Failed 1st Check
27/12/2013 17:51:55 Message: Application Started
27/12/2013 17:51:56 Message: Network Check Started
27/12/2013 17:52:06 Message: Network Established
27/12/2013 17:52:06 Message: Running Startup Checks
27/12/2013 17:52:08 Message: Decompiling
27/12/2013 17:52:08 Message: Compiling
27/12/2013 17:52:08 Message: Startup Checks Complete..Loading Splash
27/12/2013 17:52:10 Message: Splash Loaded
27/12/2013 17:52:12
-
Dec 27th, 2013, 07:58 PM
#19
Re: Read last 200 lines of a text file
What do you intend to do with the returned value? If you're putting it in a textbox or just writing it to a new file, a string formatted with beeline characters would be ok
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 27th, 2013, 07:59 PM
#20
Re: Read last 200 lines of a text file
That should be newline + not beeline. Excuse my predictive typing
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 27th, 2013, 08:03 PM
#21
Thread Starter
Addicted Member
Re: Read last 200 lines of a text file
basically if a client has a support issue, the client runs a diagnostics report through the software which gets emailed to me via a httpwebrequest - i want to append the last 200 lines of the log file to the diagnostics report
-
Dec 27th, 2013, 08:05 PM
#22
Thread Starter
Addicted Member
Re: Read last 200 lines of a text file
I can return the whole logfile which i've tested successfully - however if the logfile gets big it will fail to send
-
Dec 27th, 2013, 08:09 PM
#23
Thread Starter
Addicted Member
Re: Read last 200 lines of a text file
This returns the whole logfile perfectly
Code:
Public Function ReadLog() As String
If File.Exists(Strings.LogFile) Then
Using tr As TextReader = New StreamReader(Strings.LogFile)
Return tr.ReadToEnd
End Using
End If
End Function
However i'm worried it will cause issues when the logfile gets large
-
Dec 27th, 2013, 08:11 PM
#24
Re: Read last 200 lines of a text file
Code:
' Read last 200 lines of log file
Public Function ReadLog() As String
Dim lines() As String = IO.File.ReadAllLines("C:\logfile.txt")
If lines.Length <= 200 Then Return String.Join(Environment.NewLine, lines)
Dim lines200(199) As String
Array.Copy(lines, lines.Length - 200, lines200, 0, 200)
Return String.Join(Environment.NewLine, lines200)
End Function
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 27th, 2013, 08:16 PM
#25
Thread Starter
Addicted Member
Re: Read last 200 lines of a text file
Superb Paul
Works a treat - many thanks for your help guys - it really is appreciated!!
-
Dec 27th, 2013, 08:18 PM
#26
Re: Read last 200 lines of a text file
Use a StringBuilder ....
vb.net Code:
Dim sb As New System.Text.StringBuilder
sb.Append(DiagnosticReport) ' the text as a string
sb.AppendLine()
Dim lines As String() = IO.File.ReadAllLines(Strings.LogFile)
Dim lines200 = getLast200Elements(lines)
For each Ln in Lines200
sb.AppendLine(Ln)
Next
Return sb.ToString
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Dec 27th, 2013, 08:19 PM
#27
Re: Read last 200 lines of a text file
Or do it that way ... sigh! Must learn to type faster!
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Dec 27th, 2013, 08:20 PM
#28
Thread Starter
Addicted Member
Re: Read last 200 lines of a text file
thanks dunfiddlin - Paul beat you to it - however i do appreciate your help!
Many thanks
-
Dec 28th, 2013, 12:34 PM
#29
Re: [RESOLVED] Read last 200 lines of a text file
Ah yes, .paul beat me to it. They'll be writing that on my gravestone (I hope .... mwhaha!!!)
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Dec 29th, 2013, 03:52 PM
#30
Re: [RESOLVED] Read last 200 lines of a text file
I'll look at it tomorrow morning. I did it right before I left for the day Friday but in the text file I had 252 lines. Displayed where it started and ended, I may have missed something. I wrote it on VS 2012. I will put out a tested version for you tomorrow morning (EST).
Please remember next time...elections matter!
-
Dec 29th, 2013, 05:10 PM
#31
Re: [RESOLVED] Read last 200 lines of a text file
I see it is resoved and my way was the clunkiest but I fixed the error and made testing easier (for me).
Code:
Dim lines As String()
Dim iCount As Integer
Dim Inputfile As System.IO.StreamWriter
Dim Outputfile As System.IO.StreamWriter
Dim iStart As Integer = 1
Dim strTextLine As String
If System.IO.File.Exists("C:\Input.txt") = True Then
System.IO.File.Delete("C:\Input.txt")
End If
If System.IO.File.Exists("C:\Output.txt") = True Then
System.IO.File.Delete("C:\Output.txt")
End If
Inputfile = My.Computer.FileSystem.OpenTextFileWriter("c:\Input.txt", True)
Outputfile = My.Computer.FileSystem.OpenTextFileWriter("c:\Output.txt", True)
Using Inputfile
For i = 1 To 300
Inputfile.WriteLine(i)
Next
End Using
lines = IO.File.ReadAllLines("C:\Input.txt")
iCount = lines.Length
iStart = iCount - 200
For i = iStart To iCount - 1
strTextLine = System.IO.File.ReadAllLines("C:\Input.txt")(i)
Outputfile.WriteLine(strTextLine)
Next
Outputfile.Close()
End Sub
Please remember next time...elections matter!
-
Dec 30th, 2013, 05:29 AM
#32
Re: [RESOLVED] Read last 200 lines of a text file
Just for kicks, this could all be done in two lines:
Code:
Dim lines = IO.File.ReadAllLines("file path here")
Dim text = String.Join(Environment.NewLine,
lines,
Math.Max(0, lines.Length - 200),
Math.Min(200, lines.Length))
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
|