Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace in parts of file only 2

Status
Not open for further replies.

hugh999

MIS
Nov 29, 2001
129
IE
Hi, I have this code that works fine for replacing in TXT file. I need to change the code so only to perform the replace in groups of text.

Code:
        Dim Fs As FileStream = New FileStream("C:\report.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
        Dim sw As New StreamWriter(Fs)
        Dim sr As New StreamReader(Fs)
        Dim str As String
        str = sr.ReadToEnd

        str = str.Replace("Item-ID", "Stock-ID")

        Fs.Position = 0
        Fs.SetLength(str.Length)

        sw.Write(str)
        sw.Flush()
        sw.Close()
        Fs.Close()

Example: Replace only in text after the line starts with “Report-Output” and before the line that stars with “Report End”

Report-Output
?????
??????????????
???????
??
Report End

Help with the code would be appreciated or a link to sample code that i can work with.
Thanks
 
I have also tried playing around with regex but no joy

Code:
 Dim fileText As String = System.IO.File.ReadAllText("C:\report.txt")

        Dim pattern As String = Regex.Escape("Report-Output" & "(?s)(.+?)" & "Report End")

        modifiedText = System.Text.RegularExpressions.Regex.Replace( _
        pattern, "Item-ID", "Stock-ID", RegexOptions.IgnoreCase)

        File.WriteAllText("C:\report.txt", modifiedText)
 
I would just deploy a lazy approach: Read line by line from C:\report.txt and write to C:\report1.txt This way you have full control of what’s going on.
Then, at the end, Delete report.txt and rename report1.txt to report.txt


Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Hi, I did attempt to use the split method (samples found on web) by reading each line but to be honest as my coding experience is limited, I came up blank on what to do next.

Code:
Dim sr As IO.StreamReader = ("c:\report.txt")   

Dim contents As New List(Of String)

Do While sr.Peek <> -1   
contents.Add(sr.ReadLine)    
Loop    
      
For Each MyString As String In contents    
  
If MyString.StartsWith("Report-Output") Then     
str_SplitLine = Split(MyString, "Report-Output")

Then I do not know what to do next.

I need help with the code or sample code so help me on my way.

Thanks
 
Can any of the experts assist me with this as I have come to a dead end

Thanks
 

I also would do what Andrzejek suggested. Here's how I would do it:

Dim sr As IO.StreamReader = ("c:\report.txt")

Dim sw As IO.StreamWriter = New StreamWriter("C:\report1.txt")

'Read until "Report-Output" found
Dim MyString As String = ""

Do

MyString = sr.ReadLine()

sw.WriteLine(MyString)

Loop Until MyString.StartsWith("Report-Output")

Do

If Not MyString.StartsWith("Report End") Then
MyString = sr.ReadLine()

MyString = 'make whatever changes you need

sw.WriteLine(MyString)
End If

Loop Until MyString.StartsWith("Report End")

Do While sr.Peek <> -1
sw.WriteLine(sr.ReadLine)
Loop

sr.Close()
sw.Close()

System.IO.File.Delete("C:\report.txt")

Microsoft.VisualBasic.FileIO.FileSystem.RenameFile("C:\report1.txt", "C:\report.txt")




I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks for the help, it is exactly what i was trying to acheive.

Thanks
 
You have been a member of TT for about 13 years and I hope you get some good help from here. Yet you gave just one star so far. When you mark the post with a star, you say: “Thank you for your helpful post” and also other TT members can see which post was the solution to your problem.
[blue]
Like this post?
Star it![/blue]



Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Sorry, forgot to add star, it has been a while since using the forum.

May I also ask about the code sample supplied, it works great if the file contains one block of text as per my initial query, but if there are more than 1 blocks of text, it will only do the replace in the first block of text and not on the rest of blocks of text.

I tried to play around with the DO loops so to keep searching the file until the end of stream, but not having much luck. I end up with a never ending loop or nothing happens at all.

Thanks
 

Something like this should do it:

Do While sr.Peek <> -1

MyString = sr.ReadLine()

If MyString.StartsWith("Report-Output") Then

Do
MyString = sr.ReadLine()

If Not MyString.StartsWith("Report End")
MyString = 'make whatever changes you need
sw.WriteLine(MyString)
Else
sw.WriteLine(MyString)
End If

Loop Until MyString.StartsWith("Report End")

Else

sw.WriteLine(MyString)

End If

Loop




I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks again with the help with the code, it does make the replacement in all blocks of text but unfortunately, it does not write back all lines of text. The line of text it omits, is “Report-Output”.

Here is the code I have
Code:
Dim sr As IO.StreamReader = New StreamReader("C:\\sample\report.txt")

        Dim sw As IO.StreamWriter = New StreamWriter("C:\sample\1-report.txt")

        Dim MyString As String = ""
 
        Do While sr.Peek <> -1

            MyString = sr.ReadLine()

            If MyString.StartsWith("Report-Output") Then

                Do
                    MyString = sr.ReadLine()

                    If Not MyString.StartsWith("Report End") Then

                        MyString = MyString.Replace("Item-ID", "Stock-ID")

                        sw.WriteLine(MyString)
                    Else
                        sw.WriteLine(MyString)
                    End If

                Loop Until MyString.StartsWith("Report End")

            Else

                sw.WriteLine(MyString)

            End If

        Loop

        sr.Close()
        sw.Close()

Thanks
 
Did you try:

Code:
If MyString.StartsWith("Report-Output") Then[blue]
    sw.WriteLine(MyString)[/blue]
    Do
        ....

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top