Results 1 to 13 of 13

Thread: Adding line between two lines

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    243

    Adding line between two lines

    M48
    METRIC,LZ
    VER,1
    FMAT,2
    T00
    T168
    T169
    T170
    T176
    T250
    DETECT,ON



    I want to add some text beside the text which contain "T" I have the same value below the line DETECT,ON but i just want to add text to the lines between FMAT,2 and DETECT,ON. Can i use indexOf methode to get the lines between FMAT,2 and DETECT,ON

    For now i used this code to add text
    Code:
    Dim filename As String = strFileName
            Dim tfLines() As String = System.IO.File.ReadAllLines(filename)
    
            If File.Exists("C:\kaya.txt") Then
    
                Console.WriteLine("{0} already exists.", "C:\kaya.txt")
                Return
            End If
    
            Dim objwriter As StreamWriter
    
    
            objwriter = File.AppendText("C:\kaya.txt")
    
            For x As Integer = 1 To tfLines.GetUpperBound(0)
                                If tfLines(x).Contains("T240") Then
                        objwriter.WriteLine("T240" & "C3.175F012S27H2000")
    
                    ElseIf tfLines(x).Contains("T00") Then
                        tfLines(x).Replace("T00", "")
    
    
                    ElseIf tfLines(x).Contains("T239") Then
                        objwriter.WriteLine("T239 " & "C0.95F034S55H1000")
    
                    ElseIf tfLines(x).Contains("T236") Then
                        objwriter.WriteLine("T236" & "C1.2F029S48H2000")
    
    
                    ElseIf tfLines(x).Contains("T234") Then
                        objwriter.WriteLine("T234" & "C1.6F029S48H2000")
    
    
                    ElseIf tfLines(x).Contains("T232") Then
                        objwriter.WriteLine("T232" & "C1.95F022S37H2000")
    
    
                    ElseIf tfLines(x).Contains("T230") Then
                        objwriter.WriteLine("T230" & "C2.4F017S3H2000")
    
    
    
                    ElseIf tfLines(x).Contains("T229") Then
                        objwriter.WriteLine("T229" & "C3.3F005S16H2000")
    End If
    
                    objwriter.WriteLine(tfLines(x))
    
                      Next
    
            objwriter.Close()

    I want the text to be added to the T value above the line DETECT,ON. because this code add the text beside T whenever it finds the T value.

    I dont know how add line only between line FMAT,2 and DETECT,ON?

  2. #2
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Adding line between two lines

    if those lines are always like that and don't change position, then have your For loop end at the index of the "DETECT,ON" entry instead of going to .GetUpperBound(0)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    243

    Re: Adding line between two lines

    I cannot depend on position. Because it always change. I must cosider only the Two lines

  4. #4
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Adding line between two lines

    if this works:

    vb Code:
    1. ElseIf tfLines(x).Contains("T229") Then
    2.                     objwriter.WriteLine("T229" & "C3.3F005S16H2000")

    then you should be able to:

    vb Code:
    1. ElseIf tfLines(x).Contains("DETECT, ON") Then
    2.                     Exit For

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    243

    Re: Adding line between two lines

    Still give me a same problem. It not exit after the line DETECT

  6. #6
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Adding line between two lines

    how about.. if it contains "DETECT, ON", then set x (your For loop counter variable) equal to tfLines.GetUpperBound(0)

    then, the next iteration should not take place because it will have reached the "end" of the for loop.

    vb.net Code:
    1. ElseIf tfLines(x).Contains("DETECT, ON") Then
    2.      x = tfLines.GetUpperBound(0)
    3. End If
    4. Next

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    243

    Re: Adding line between two lines

    Acctually when i used this code
    Code:
    ElseIf tfLines(x).Contains("T229") Then                    objwriter.WriteLine("T229" & "C3.3F005S16H2000")
    It will search for the T229 througout the file then if it find it will add the value. So i think there is no point to exit for or set upperbound to 0. Because before it reach the line DETEC,ON it already add the text to all T value.

    I think this is the problem


    But i got no idea how to solve this problem.

    I tryed to used Do loop But not working also.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    243

    Re: Adding line between two lines

    This is my text file:

    M48
    METRIC,LZ
    VER,1
    FMAT,2
    T00
    T168
    T169
    T170
    T176
    T250
    DETECT,ON
    %
    G93X0Y29
    T250
    X22Y0
    X0Y0
    T169
    X016038Y11991
    X203998Y11991
    X203998Y-134725
    X216888Y105277
    X216Y-108
    X003148Y105277
    X004Y-108
    T168
    X216888Y105277
    X216Y-108
    X003148Y105277
    X004Y-108
    T170
    X11Y1535
    T176
    X119035Y-126978
    X024039Y-119104
    X024039Y-120374
    X024801Y-117961
    X048118Y-124438
    T00


    I want to add text in this part of line only

    FMAT,2
    T00
    T168
    T169
    T170
    T176
    T250
    DETECT,ON

    So it will look like something like this

    FMAT,2
    T00
    T168xxx
    T169xxx
    T170
    T176
    T250xxx
    DETECT,ON

    I used this code to do like this

    Code:
    Dim filename As String = strFileName
            Dim tfLines() As String = System.IO.File.ReadAllLines(filename)
    
            If File.Exists("C:\kaya.txt") Then
    
                Console.WriteLine("{0} already exists.", "C:\kaya.txt")
                Return
            End If
    
            Dim objwriter As StreamWriter
    
    
            objwriter = File.AppendText("C:\kaya.txt")
    
            For x As Integer = 0 To tfLines.GetUpperBound(0)
    
    
    
    
    
                If tfLines(x).Contains("T240") Then
                    objwriter.WriteLine("T240" & "C3.175F012S27H2000")
    
                ElseIf tfLines(x).Contains("T00") Then
                    tfLines(x).Replace("T00", "")
    
    
                ElseIf tfLines(x).Contains("T239") Then
                    objwriter.WriteLine("T239 " & "C0.95F034S55H1000")
    
                ElseIf tfLines(x).Contains("T236") Then
                    objwriter.WriteLine("T236" & "C1.2F029S48H2000")
    
    
                ElseIf tfLines(x).Contains("T234") Then
                    objwriter.WriteLine("T234" & "C1.6F029S48H2000")
     Else
    
    
                    objwriter.WriteLine(tfLines(x))
                End If
    
    
    
            Next
    
            objwriter.Close()

    This code not only add text in the part that i want but it add also in below part. Like this

    METRIC,LZ
    VER,1
    FMAT,2
    T00
    T168xxx
    T169
    T170xxx
    T176
    T250
    DETECT,ON
    %
    G93X0Y29
    T250
    X22Y0
    X0Y0
    T169
    X016038Y11991
    X203998Y11991
    X203998Y-134725
    X216888Y105277
    X216Y-108
    X003148Y105277
    X004Y-108
    T168xxx
    X216888Y105277
    X216Y-108
    X003148Y105277
    X004Y-108
    T170xxx
    X11Y1535
    T176
    X119035Y-126978
    X024039Y-119104
    X024039Y-120374
    X024801Y-117961
    X048118Y-124438


    How can i solve this problem?

  9. #9
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Adding line between two lines

    did you try my last idea? seems like it should've worked.. once it finds your line, it would set the loop's counter to the end.

    EDIT: you know, I'm surprised none of the regex pros on here hasn't chimed in yet. I'm sure there's a much easier way to do what you need than what you've tried or what i've suggested. I'll bump for ya, maybe someone will chime in.

  10. #10
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Adding line between two lines

    1. Read the file into a string array using file.readAllLines (you already do this)
    2. Add this array to a List(of String)
    Code:
    Dim lineList As New List(Of String)
    lineList.AddRange(tfLines)
    3. Use List.IndexOf function to find the line of interest. In this case, you want to find
    where "DETECT,ON" is
    Code:
    Dim idx As Integer = lineList.IndexOf("DETECT,ON")
    4. Now you're ready to insert whatever you like just before the "DETECT,ON" line
    Code:
    If idx > 0 Then
        Dim insertLine As String = "whatever you want on this line"
        lineList.Insert(idx - 1, insertLine)
    End If
    5. Finally, write that lineList back to your file
    Code:
    System.IO.File.WriteAllLines("file Path here", lineList.ToArray)
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  11. #11

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    243

    Re: Adding line between two lines

    If idx > 0 Then
    Dim insertLine As String = "whatever you want on this line"
    lineList.Insert(idx - 1, insertLine)
    End If
    I dont understand this part.

    In the above code how i must used this line

    Code:
    If tfLines(x).Contains("T240") Then
                    objwriter.WriteLine("T240" & "C3.175F012S27H2000")
    
                ElseIf tfLines(x).Contains("T00") Then
                    tfLines(x).Replace("T00", "")
    Can you explain more to me

  13. #13
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    Re: Adding line between two lines

    Quote Originally Posted by kayatri View Post
    Acctually when i used this code
    Code:
    ElseIf tfLines(x).Contains("T229") Then                    objwriter.WriteLine("T229" & "C3.3F005S16H2000")
    It will search for the T229 througout the file then if it find it will add the value. So i think there is no point to exit for or set upperbound to 0. Because before it reach the line DETEC,ON it already add the text to all T value.

    I think this is the problem


    But i got no idea how to solve this problem.

    I tryed to used Do loop But not working also.
    Wrong. X gets incremented by 1 each time it loops. Your elseIfs only look at line x, not all the lines at the same time.

    If you did what Stateofidleness suggested and put
    Code:
     If tfLines(x).Contains("DETECT,ON") Then Exit For
    it works perfectly. I know because I tried it. I suggest you do the same without wasting other peoples time.

    The title of this post is extremely misleading, which is why Stanav gave you a solution which doesn't do what you intend to do.
    Runesmith

    The key to getting the right answer is asking the right question

    I just realized: good health is merely the slowest possible rate at which one can die

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