Results 1 to 7 of 7

Thread: Add Row Number to Text File

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    4

    Add Row Number to Text File

    I'm have a hard time with VB and try to use SQL to do the things I need to do. I'm stuck on this one and need a row number added to a text file. I found this code searching the internet, and think it will work. The problem I have is I can't get it working in VB. I'm getting it needs a subMain error. I'm not sure where (how) the file is named. Should this have a button?

    Here is an example of what I'm trying to accomplish.

    I have an ascii file that has records of varying length:

    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    bbbbbbbbbbbbbbbbbbb
    ccccccccccccccccccccccccccccccccccccccccc
    dddddddddddddddddddddd
    eeeeeee
    fff
    ggggggggggggggggggggggggggggggggggggggggggggggggg

    I need to add a leading sequence number to these transactions, with padded zeros. So the output will look like this:

    000001aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    000002bbbbbbbbbbbbbbbbbbb
    000003ccccccccccccccccccccccccccccccccccccccccc
    000004dddddddddddddddddddddd
    000005eeeeeee
    000006fff
    000007ggggggggggggggggggggggggggggggggggggggggggggggggg

  2. #2

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    4

    Re: Add Row Number to Text File

    Here is the code that I think will work. I think it is missing something, like a button or something. I get a needs subMain error.
    VB Code:
    1. Private Sub AddLineNumbers(ByVal theFile As String)
    2. If File.Exists(theFile) Then
    3. Dim file1Sr As New StreamReader(theFile)
    4. Dim theLineNumber As Integer = 1
    5. Dim aLine As String
    6. Dim tmpFile As String = "./log.tmp"
    7.  
    8. ' determine if tmp file exists
    9. If File.Exists(tmpFile) Then
    10. File.Delete(tmpFile)
    11. End If
    12. 'create the tmp file
    13. Dim fStream As FileStream
    14. Try
    15. fStream = File.Create(tmpFile)
    16. Catch ex As Exception
    17. MessageBox.Show(ex.Message, ex.GetType.Name, MessageBoxButtons.OK)
    18. End Try
    19. fStream.Close()
    20.  
    21. 'create the streamwriter for the tmp file
    22. Dim fileSw As StreamWriter
    23. fileSw = File.AppendText(tmpFile)
    24.  
    25. While file1Sr.Peek() <> -1
    26. aLine = file1Sr.ReadLine
    27. If aLine.Length > 0 Then
    28. If aLine.Chars(0) = "(" Or aLine.Split(":")(0) = "Server" Then
    29. fileSw.WriteLine(theLineNumber.ToString & " - " & aLine)
    30. theLineNumber += 1
    31. Else
    32. fileSw.WriteLine(aLine)
    33. End If
    34. Else
    35. fileSw.WriteLine(aLine)
    36. End If
    37. End While
    38.  
    39. file1Sr.Close()
    40. fileSw.Close()
    41.  
    42. Try
    43. File.Copy(tmpFile, theFile, True)
    44. Catch ex As Exception
    45. MessageBox.Show(ex.Message, ex.GetType.Name, MessageBoxButtons.OK)
    46. Exit Sub
    47. End Try
    48.  
    49. File.Delete(tmpFile)
    50. Else
    51. MessageBox.Show("File '" & theFile & "' Not Found!", "File Error", MessageBoxButtons.OK)
    52. End If
    53. End Sub
    Last edited by Hack; Feb 16th, 2006 at 08:00 AM. Reason: Added [vbcode] [/vbcode] tags and for more clarity.

  3. #3
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Add Row Number to Text File

    this looks like you dint specify the startup form in your project->properties->startup form. try and let me know....
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    4

    Re: Add Row Number to Text File

    OK I think I did it right. I did a standard EXE, then dropped this code into the Form1 code area. When I try to make an Project1.exe, I get a compile syntex error.

  5. #5
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Add Row Number to Text File

    post the code where you get the syntax error...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Add Row Number to Text File

    Since your code is .NET, post your question in the .NET forum as well.

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2006
    Posts
    4

    Re: Add Row Number to Text File

    LOL, I thought is was VB 6 code.

    How much different would this code be in VB6?
    Last edited by swtrans; Feb 16th, 2006 at 06:02 AM.

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