Add line in txt file offset(3) lines from line contains Product

kdj

Member
Joined
Oct 26, 2015
Messages
5
Programming Experience
1-3
Hi,

Im trying to add a line on offset of 3 lines.
Hope somebody can put me in the right direction.
thanks in advance,

VB.NET:
 Dim myStreamReaderL1 As System.IO.StreamReader
        Dim myStream As System.IO.StreamWriter
        Dim rplce As String = "Product end" & vbCrLf & "Product" 
        




        Dim myStr As String


        myStreamReaderL1 = System.IO.File.OpenText(RootFolder.Text & "\" & "Temp.txt")
        myStr = myStreamReaderL1.ReadToEnd()
        myStreamReaderL1.Close()
        myStreamReaderL1.Dispose()
        myStr = myStr.Replace("Product", rplce)
            
        ( skip 3 lines and add: "My new Line" )


        myStream = System.IO.File.CreateText(RootFolder.Text & "\" & "Temp.txt")
        myStream.WriteLine(myStr)
        myStream.Close()
 
Hi and Welcome to the Forum,

You can use the String.Format Method to build the string that you are trying to create. Have a play with this example:-

Dim myStr = "Some String with the Word Product In It"
Dim rplce As String = String.Format("Product end{0}Product{0}{0}{0}{1}", Environment.NewLine, "My new Line")
myStr = myStr.Replace("Product", rplce)
 
MsgBox(myStr)


Hope that helps.

Cheers,

Ian
 
Hi Ian,

it almost worked but there is one line after Product which i have to skip (delete always). this line is always there on same place. sorry maybe i explained wrong.
but i only know that it is always the line after product.

Cheers,
Kdj
 
Hi,

That has added a little bit of complexity to the question. Have a look at this to see if it now answers the question in full:-

Dim myStr As String = String.Format("Some String with the Word Product In It{0}And We Need to Get Rid Of This Line{0}But Keep Other Lines??", Environment.NewLine)
MsgBox(myStr)
 
Dim myNewStringToAdd As String = "My new Line"
Dim rplce As String = String.Format("Product end{0}Product{0}{0}{0}{1}", Environment.NewLine, myNewStringToAdd)
myStr = myStr.Replace("Product", rplce)
MsgBox(myStr)
 
'Split the String into Lines
Dim allLines As List(Of String) = myStr.Split(New String() {Environment.NewLine}, StringSplitOptions.None).ToList
'Identify where we added the New String
Dim indexToDismiss As Integer = allLines.FindIndex(Function(x) x.Contains(myNewStringToAdd)) + 1
'Get the Lines Preceeding the Line we want to delete
Dim preceedingLines As String() = allLines.Take(indexToDismiss).ToArray
'Get the Lines Following the Line we want to delete
Dim followingLines As String() = allLines.Skip(indexToDismiss + 1).ToArray
'Add the Two New Arrays Together and recreate the Final String
Dim newlineArrray As String() = preceedingLines.Concat(followingLines).ToArray
myStr = String.Join(Environment.NewLine, newlineArrray)
MsgBox(myStr)


I have added a simple message box at each modification of the myStr variable so that you can see what is happening at each stage of the code. The important thing to note here is that this is all dependant on being able to identify the specific index in an array that represents the information that you added since this then defines the boundaries of what needs to be kept.

Hope that helps.

Cheers,

Ian
 
Hi,

i don't get that code working correct, i will play arround with it. maybe i can get it working.
I split the temp file later in my code to different files. and the only thing i am missing is the First line in each file.
maybe i can do it on an easier way.

Cheers,

KDJ
 
Hi,

this is how the file looks like always, only product can be more depents on how many products there are in the file.

VB.NET:
<Satz SatzNr="2" Function1="-1" Function2="1" Function3="2" Function4="50" Function5="50" Function6="-1" Function7="-1" Function8="-1" Function9="0" Function10="0" Reiterations="1" GraphicType="1" CalculateValueTyp="0" Height="6" L1="3000" L2="3000" L3="0" W1="90" W2="90" W3="-1"/>
 
<Satz SatzNr="3" Function1="-1" Function2="1" Function3="2" Function4="50" Function5="50" Function6="-1" Function7="-1" Function8="-1" Function9="0" Function10="0" Reiterations="1" GraphicType="1" CalculateValueTyp="0" Height="6" L1="3000" L2="3000" L3="0" W1="90" W2="90" W3="-1"/>
</Saetze><Material/></Programm></Programme>
Product
<Satz SatzNr="4" Function1="-1" Function2="1" Function3="2" Function4="50" Function5="50" Function6="-1" Function7="-1" Function8="-1" Function9="0" Function10="0" Reiterations="1" GraphicType="6" CalculateValueTyp="4098" Height="6" L1="6000" L2="5659,72" L3="0" W1="-2820" W2="1" W3="-1"/>
 
<Satz SatzNr="5" Function1="-1" Function2="1" Function3="2" Function4="50" Function5="50" Function6="-1" Function7="-1" Function8="-1" Function9="0" Function10="0" Reiterations="1" GraphicType="1" CalculateValueTyp="0" Height="6" L1="3000" L2="3000" L3="0" W1="90" W2="90" W3="-1"/>
</Saetze><Material/></Programm></Programme>

and this is how it needs to be

VB.NET:
<Satz SatzNr="2" Function1="-1" Function2="1" Function3="2" Function4="50" Function5="50" Function6="-1" Function7="-1" Function8="-1" Function9="0" Function10="0" Reiterations="1" GraphicType="1" CalculateValueTyp="0" Height="6" L1="3000" L2="3000" L3="0" W1="90" W2="90" W3="-1"/>
 
<Satz SatzNr="3" Function1="-1" Function2="1" Function3="2" Function4="50" Function5="50" Function6="-1" Function7="-1" Function8="-1" Function9="0" Function10="0" Reiterations="1" GraphicType="1" CalculateValueTyp="0" Height="6" L1="3000" L2="3000" L3="0" W1="90" W2="90" W3="-1"/>
</Saetze><Material/></Programm></Programme>
#Product
#<Satz SatzNr="4" Function1="-1" Function2="1" Function3="2" Function4="50" Function5="50" Function6="-1" Function7="-1" Function8="-1" Function9="0" Function10="0" Reiterations="1" GraphicType="6" CalculateValueTyp="4098" Height="6" L1="6000" #L2="5659,72" L3="0" W1="-2820" W2="1" W3="-1"/>
(my new line)
<Satz SatzNr="5" Function1="-1" Function2="1" Function3="2" Function4="50" Function5="50" Function6="-1" Function7="-1" Function8="-1" Function9="0" Function10="0" Reiterations="1" GraphicType="1" CalculateValueTyp="0" Height="6" L1="3000" L2="3000" L3="0" W1="90" W2="90" W3="-1"/>
</Saetze><Material/></Programm></Programme>



splitting it on product -2 lines already working. i get the total number of files. if the line (my new line) is added the split files i generate are exactly what i need.
the lines which starts with # i skip with splitting the file into multiple files.
Maybe you have another suggestion to get there.

Cheers,

Kdj
 
You are kidding right?

I have given you two posts to guide you based on what you have told us and now, since it does NOT WORK exactly how you want it to, you post your complete file and expect us to work out for ourselves how to fix your problem for you?

This is not how I work and good luck on fixing your issues.

Cheers,

Ian
 
Example using your last post:
        Dim path = IO.Path.Combine(RootFolder.Text, "Temp.txt")
        Dim lines = IO.File.ReadAllLines(path)
        For i = 0 To lines.Count - 1
            If lines(i) = "Product" Then
                lines(i) = "#Product"
                lines(i + 1) = "#" & lines(i + 1)
                lines(i + 2) = "(My new Line)"
            End If
        Next
        IO.File.WriteAllLines("new.txt", lines)

You're not mangling a xml file, are you? :devilish:
 
Hi,

@ Ian, i did not mean that you guys have to solve my problem i asked for another suggestion to do it. what is easier to do for me. cause i could not manage it that way.
and why i post the file was just to explain more how to do it.

thanks JohnH i will play arround with that code so i can learn more.

Cheers,

KDJ

#Closed
 
Back
Top