Home > Enterprise >  find character and Insert a new line in the file
find character and Insert a new line in the file

Time:01-06

I am looking to read a file line by line and once I reach a line that doesn't contain the same characters in columns 65 - 67 I want to insert a string line 2 lines above that. I am using VB.NET

an example of the file is below.

Test..07906089308920197475778487547574957..Test.64759403033840..04392150....................0000..................839330758............. Test..07906089308920197475778487547574958..00000000000000000000.........00000000000........0000000000.000000000000000000000......00.....
'--INSERT STRING "NEW BLOCK" Test..07906089308920197475778487547575638..Test.64759403033841..04492205....................0000..................839330775............. Test..07906089308920197475778487547575785..00000000000000000000.........00000000000........0000000000.000000000000000000000......00.....
'--INSERT STRING "NEW BLOCK" Test..07906089308920197475778487547576859..Test.64759403033840..04592160....................0000..................839330758............. Test..07906089308920197475778487547576940..00000000000000000000.........00000000000........0000000000.000000000000000000000......00.....

So far I have written code that reads the file line by line. I am unsure how to insert a string line to a file where characters in columns 65-67 are different any help would be great. Thanks

   Protected Sub UploadFile(sender As Object, e As EventArgs)
    Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
    Dim filePath As String = Server.MapPath("~/Files/") & fileName
    FileUpload1.SaveAs(filePath)

    '--Read block of lines then insert line for each new block
    For Each line In IO.File.ReadLines(filePath)
        'If line.Substring(65, 3) <> " " Then '<> to previous substring

            '--Insert new line 

        'End If
    Next

End Sub



 <asp:FileUpload ID="FileUpload1" runat="server" />
 <asp:Button ID="Button1" Text="Upload File" runat="server" OnClick="UploadFile" />

CodePudding user response:

Something like this.

    Dim lines As List(Of String) = IO.File.ReadLines(filePath).ToList
    Dim idx As Integer = 0
    Do While idx < lines.Count - 2
        Dim thisLine As String = lines(idx)
        Dim nextLine As String = lines(idx   1)
        If thisLine.Substring(65, 3) <> nextLine.Substring(65, 3) Then
            lines.Insert(idx   1, "NEW BLOCK")
            idx  = 1
        End If
        idx  = 1
    Loop
    Dim foo As String = String.Join(ControlChars.Cr, lines)
    IO.File.WriteAllText("path", foo)

edit - based on comments

    Dim lines As List(Of String) = IO.File.ReadLines(filePath).ToList
    Dim idx As Integer = 0
    Do While idx < lines.Count - 2
        Dim thisLine As String = lines(idx)
        Dim nextLine As String = lines(idx   1)
        Dim key As String = thisLine.Substring(65, 3).Trim
        If key <> nextLine.Substring(65, 3) AndAlso
                key = "" Then
            lines.Insert(idx   1, "NEW BLOCK")
            idx  = 1
        End If
        idx  = 1
    Loop
    Dim foo As String = String.Join(ControlChars.Cr, lines)
    IO.File.WriteAllText("path", foo)
  • Related