How to add line numbers to a text file
How to add line numbers to a text file
How to add line numbers to a text file
How to add line numbers to a text file
How to add line numbers to a text file
How to add line numbers to a text file How to add line numbers to a text file How to add line numbers to a text file How to add line numbers to a text file How to add line numbers to a text file How to add line numbers to a text file How to add line numbers to a text file How to add line numbers to a text file
How to add line numbers to a text file How to add line numbers to a text file
How to add line numbers to a text file
Go Back  Xtreme Visual Basic Talk > > > How to add line numbers to a text file


Reply
 
Thread Tools Display Modes
  #1  
Old 02-12-2005, 05:31 AM
Davearia's Avatar
Davearia Davearia is offline
Junior Contributor
 
Join Date: Jul 2004
Location: In A Perfect Cadence
Posts: 358
Question How to add line numbers to a text file


Hi All,

I have an error log file with literally hundreds, possible thousands of lines of entries.

What I am trying to do is read the file and then write a new version of this text file with a line number for each line.

I haven't got very far as yet. Just the open and save dialogs up to now I'm afraid:
Code:
Imports System.io Public Class Line_Counter Inherits System.Windows.Forms.Form Private ByteArray() As Byte Private filePath As String Private lnNumber As Int32 = 1 Private Sub Line_Counter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click Dim fileName As String If OpenFileDialog1.ShowDialog = DialogResult.OK Then Try Dim fsStream As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read) Dim brReader As New BinaryReader(fsStream) ByteArray = brReader.ReadBytes(fsStream.Length) brReader.Close() MsgBox("File read!") Catch ex As IOException MsgBox(ex.Message) End Try End If fileName = OpenFileDialog1.FileName.ToString MessageBox.Show(fileName) End Sub Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click If SaveFileDialog1.ShowDialog = DialogResult.OK Then Try filePath = SaveFileDialog1.FileName.ToString + ".txt" Dim fsStream As FileStream = New FileStream(filePath, FileMode.Create) Dim bwWriter As BinaryWriter = New BinaryWriter(fsStream) bwWriter.Write(ByteArray) bwWriter.Close() MsgBox("Wrote the copy.") Catch ex As IOException MsgBox(ex.Message) End Try End If End Sub End Class
I am not sure on this, but in the btnRead part of my code I need some sort of loop that has an integer that can be used to put at the start of every error message in the file as shown in this crappy psuedo code:
Code:
While Not EOF
    lnNum = lnNum +1
    this is added to the error line
End While
I'm sorry for the vague ideas but I am really in need of some help to get me started on this and this is the place where people help other people out.

Please help.

Thanks, Dave.
__________________
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
Reply With Quote
  #2  
Old 02-12-2005, 02:22 PM
MKoslof's Avatar
MKoslofHow to add line numbers to a text file MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

You are going to have to code this yourself, there is no way to automatically write out line numbers to a new txt file. Your logic regarding a loop is correct. You could read in the entire original file and get the total number of lines and store this in a numerical variable. Then when writing out each line again to a new file, you can start at 1 and go until the linecount, incrementing by one as each new line is written out.

Beyond this, I think we will need more information to provide better advice
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #3  
Old 02-12-2005, 02:38 PM
Davearia's Avatar
Davearia Davearia is offline
Junior Contributor
 
Join Date: Jul 2004
Location: In A Perfect Cadence
Posts: 358
Thumbs up

I don't know if this will help but I think it might. All of the lines where I want to add a number will either start with ( or Server

Here is a snippet of the file that I want to add the numbers to:
Code:
(1 row(s) affected)


(1 row(s) affected)


(1 row(s) affected)


(1 row(s) affected)

Server: Msg 547, Level 16, State 1, Line 1
INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_WizardPageControl_T_WizardPage_T'. The conflict occurred in database 'PASWizardTEST', table 'Page_T', column 'PageID'.
The statement has been terminated.
Server: Msg 547, Level 16, State 1, Line 1
INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_WizardPageControl_T_WizardPage_T'. The conflict occurred in database 'PASWizardTEST', table 'Page_T', column 'PageID'.
The statement has been terminated.
(1 row(s) affected)


(1 row(s) affected)


(1 row(s) affected)


(1 row(s) affected)
(1 row(s) affected)

Server: Msg 547, Level 16, State 1, Line 1
INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_Property_T_ControlGroup_T'. The conflict occurred in database 'PASWizardTEST', table 'ControlGroup_T', column 'ControlGroupID'.
The statement has been terminated.

(1 row(s) affected)
As you can see there all lines with no text or lines with text that does not need to have a line number.

So given this bit of information what will this psuedo loop look like?

Please help.

Thanks, Dave.
__________________
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
Reply With Quote
  #4  
Old 02-12-2005, 02:47 PM
MKoslof's Avatar
MKoslofHow to add line numbers to a text file MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

OK, I am somewhat confused . What is the set condition here to work with. If a line has NO data (blank) it is NOT written to the new file and no line number is needed correct? Or are you still writing out the blank line, but add no line number. And, if a line has data, this is automatically added to the new file and it always get a line number? What conditions are we working with and you are trying to create a new file based on the original correct?
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #5  
Old 02-12-2005, 10:04 PM
Evets Evets is offline
Newcomer
 
Join Date: Feb 2005
Posts: 20
Default

I think this routine might be close to what you're after. It does not have all the error handling you'd probably like. It creates a temp file for the log with the line numbers and them copies that temp file over the original log (which could be risky if for some reason the temp file didn't get created right).

Code:
Private Sub AddLineNumbers(ByVal theFile As String) If File.Exists(theFile) Then Dim file1Sr As New StreamReader(theFile) Dim theLineNumber As Integer = 1 Dim aLine As String Dim tmpFile As String = "./log.tmp" ' determine if tmp file exists If File.Exists(tmpFile) Then File.Delete(tmpFile) End If 'create the tmp file Dim fStream As FileStream Try fStream = File.Create(tmpFile) Catch ex As Exception MessageBox.Show(ex.Message, ex.GetType.Name, MessageBoxButtons.OK) End Try fStream.Close() 'create the streamwriter for the tmp file Dim fileSw As StreamWriter fileSw = File.AppendText(tmpFile) While file1Sr.Peek() <> -1 aLine = file1Sr.ReadLine If aLine.Length > 0 Then If aLine.Chars(0) = "(" Or aLine.Split(":")(0) = "Server" Then fileSw.WriteLine(theLineNumber.ToString & " - " & aLine) theLineNumber += 1 Else fileSw.WriteLine(aLine) End If Else fileSw.WriteLine(aLine) End If End While file1Sr.Close() fileSw.Close() Try File.Copy(tmpFile, theFile, True) Catch ex As Exception MessageBox.Show(ex.Message, ex.GetType.Name, MessageBoxButtons.OK) Exit Sub End Try File.Delete(tmpFile) Else MessageBox.Show("File '" & theFile & "' Not Found!", "File Error", MessageBoxButtons.OK) End If End Sub
Reply With Quote
  #6  
Old 02-13-2005, 04:22 AM
Davearia's Avatar
Davearia Davearia is offline
Junior Contributor
 
Join Date: Jul 2004
Location: In A Perfect Cadence
Posts: 358
Thumbs up

That worked perfect Evets!!!

Thank you very much indeed.

I'll explain what this code is for. I am running a script on SQL server and the script is 560 KB in size. When the script failed I got this very long list of all the lines that worked and the ones that failed but useless in telling me what lines failed.

Thanks to you I now have code that I can map a reference from the error log to the line of SQL that failed by using these lines numbers. I ran the application on both files and got an exact match in line numbers, 763 in both, a result.

This is only a small application but extremely useful.

Thank you once again, Dave.
__________________
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
Reply With Quote
  #7  
Old 02-13-2005, 10:19 AM
Evets Evets is offline
Newcomer
 
Join Date: Feb 2005
Posts: 20
Default

Quote:
Originally Posted by Davearia
That worked perfect Evets!!!

Thank you very much indeed.

I'll explain what this code is for. I am running a script on SQL server and the script is 560 KB in size. When the script failed I got this very long list of all the lines that worked and the ones that failed but useless in telling me what lines failed.

Thanks to you I now have code that I can map a reference from the error log to the line of SQL that failed by using these lines numbers. I ran the application on both files and got an exact match in line numbers, 763 in both, a result.

This is only a small application but extremely useful.

Thank you once again, Dave.
You're Welcome, I'm glad I could help you out. I'm still relatively new to VB.net, but I had done some file IO stuff, not too different from what you needed. If you have questions on any part of the code above don't be afraid to ask.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
How to add line numbers to a text file
How to add line numbers to a text file
How to add line numbers to a text file How to add line numbers to a text file
How to add line numbers to a text file
How to add line numbers to a text file
How to add line numbers to a text file How to add line numbers to a text file How to add line numbers to a text file How to add line numbers to a text file How to add line numbers to a text file How to add line numbers to a text file How to add line numbers to a text file
How to add line numbers to a text file
How to add line numbers to a text file
 
How to add line numbers to a text file
How to add line numbers to a text file
 
-->