Results 1 to 4 of 4

Thread: [RESOLVED] Prepending text to multiple lines in i .txt file (vb.net 2019)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    5

    Resolved [RESOLVED] Prepending text to multiple lines in i .txt file (vb.net 2019)

    Hello. I have a .txt file with around 120,000 lines forming a series of matrices or arrays (I thought it was much more, but thankfully is only this). Each line looks a bit like this:
    -2.986489e+006, 4.7354682e+008, 2.64839292e+007, -1.647282922e+004, 4.846473829e+007
    Each line has 5 values, like above. I would like to prepend the text to put in a piece of string and a linear progression of numbers at the beginning of each line, so it would go "(string) 1, (string) 2, (string) 3," one each at the beginning of each line.

    The initial file did not have delimiters in between each value, so I wrote a piece of code to put a "," at the end of each value. The code looks like this:

    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim filename As String = "C:\New Folder\Programming\Residual Stressl.txt"
            Dim endrs As String
            endrs = TextBox2.Text
            Dim endrs2 As String
            endrs2 = TextBox3.Text
            Dim x As Double = 0
            Dim fcontent As String()
            fcontent = IO.File.ReadAllLines(filename)
    
            For Each field As String In fcontent
                If field.Contains(endrs) Then
                    fcontent(x) = fcontent(x).Replace(endrs, endrs2)
                End If
                x += 1
            Next
            IO.File.WriteAllLines(filename, fcontent)
        End Sub
    I could do this because every value ends in 004, 005, 006, 007 or 008. However, I am having trouble finding a way to prepend each line. There is the function ".endswith" but I am not sure how to use that to prepend, or indeed, if it would write at the beginning of the line or at the beginning of each value. To make it more difficult, I also need to skip every second line as every second line contains just two numbers which I need to keep as they are relevant to the data, but aren't part of the array.
    I've been reading up on the Microsoft website and researching for days now, and can't find the information I need to do this. There is plenty on writing to a new file, or appending, or prepending to a piece of string in the program, but little on prepending to an existing file. Please, could anyone give me a pointer in the right direction? Thank you

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,187

    Re: Prepending text to multiple lines in i .txt file (vb.net 2019)

    Code:
    Dim fcontent As String() = IO.File.ReadAllLines(filename)
    Dim counter As Integer = 1
    
    For x As Integer = 0 to fcontent.length - 1 Step 2
        fcontent(x) = "whatever " & counter.ToString & fcontent(x)
        counter += 1
    Next

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    5

    Re: Prepending text to multiple lines in i .txt file (vb.net 2019)

    Oh yeah - that makes sense. Thanks!

    EDIT: just tried it. Worked an absolute treat. Thanks heaps. I'll be able to modify this for future reference, too.
    Last edited by Mimsy; Jul 18th, 2021 at 06:17 PM.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,187

    Re: Prepending text to multiple lines in i .txt file (vb.net 2019)

    FYI the EndsWith function is only for querying whether a string ends with a specified string. There’s also a StartsWith function for querying the start of the string.

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