Remove lines from a text file

Max_Payne

New member
Joined
Apr 2, 2004
Messages
3
Suppose you have this information:


+OK User successfully logged on.
+OK+OK
X-MimeOLE: Produced By Microsoft Exchange V6.0.6375.0
Received: by mail.amaz.com
id <01C41841.4E253060@amaz.com>; Thu, 1 Apr 2004 18:30:23 -0500
MIME-Version: 1.0
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: base64
content-class: urn:content-classes:message
Subject: Are you a little bit chilly?
Date: Thu, 1 Apr 2004 18:30:23 -0500
Message-ID: <C25026C097DC8E47B27ECB4448821681DC0160@amaz.com>
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
Thread-Topic: Are you a little bit chilly?
Thread-Index: AcQYQU4bjqPf1UELSi2ZIqeEEkqqSA==
From: "Test" <test@amaz.com>
To: "Joe King" <Joe.King@amaz.com>

PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv
L0VOIj48SFRNTD48SEVBRD48TUVUQSBIVFRQLUVRVUlWPSJDb250ZW50LVR5cGUiIENPTlRFTlQ9
InRleHQvaHRtbDsgY2hhcnNldD11dGYtOCI+PC9IRUFEPjxCT0RZPjxESVY+Q29tZSB0byB0aGUg
Qi1TaWRlIENhZmUgdG9uaWdodCBmb3IgdGhlIEJBTkQgClNIT1dDQVNFLiZuYnNwOyBUaGUgc2hv
dyBpcyBmcmVlLCBhbmQgc3RhcnRzIGF0IDcuPC9ESVY+CjxESVY+Jm5ic3A7PC9ESVY+CjxESVY+
Jm5ic3A7PC9ESVY+CjxESVY+UmVtZW1iZXIsIGV2ZXJ5dGhpbmcgb24gb3VyIG1lbnUgKGluY2x1
ZGluZyBob3QgY29mZmVlIGZvciBvbmx5ICQuNzUgb3IgYWxsIApuaWdodCBmb3IgJDEpIGlzIGEg
ZG9sbGFyIG9yIGxlc3MuJm5ic3A7IFdlIGFyZSBmdWxseSBzdG9ja2VkIHdpdGggYWxsIHlvdXIg
CmZhdm9yaXRlIHdhcm0gc25hY2tzIGxpa2UgcHJldHplbHMgYW5kIEhvdCBQb2NrZXRzLiZuYnNw
OyBDb21lIGRvd24sIGdyYWIgc29tZSAKd2FybSBmb29kIGFuZCBhIGRyaW5rLCBhbmQgZW5qb3kg
dGhlIHNob3chPC9ESVY+CjxESVY+Jm5ic3A7PC9ESVY+PC9CT0RZPjwvSFRNTD4=
.

and you want to remove the header of the file, that is from "+OK" to "To: "Joe King" <Joe.King@amaz.com>", how can you do it, and then save the file? I tried using the remove function, but it didnt work. One key information that i was working with is that there is a new line between both sections, but i couldnt figure out a way to use it!

thanks,
 
You need to use a StreamReader to read the whole file into a string variable... then Remove the lines from the string variable that you dont want. When you are done, use a StreamWriter to write the existing string back to the file. :)
 
theres a method of the string class, replace("word to be replaced", "word to put in place of old one"). i think thats the format of it. just type in the line you dont want as what you want replaced, and make a blank string to replace it. not sure but i think this will work, as i have used i tn c#
 
Wouldnt that require him to know everything that appears between "+OK" and "Joe King" (oops almost put schmo)?
Maybe, with the .IndexOf properties of the string, he can find the beginning and end locations:
Begin = text.IndexOf("+OK")
Endpt = text.IndexOf("Joe King") Real name or are you jokeing?
Endpt = text.IndexOf(System.Environment.NewLine, Endpt) Finds the next end of the line after "Joe King".
text.Remove(Begin, Endpt - Begin)
something like that. :)
 
well, hed have to create a file stream, and read in the file into a string. he wouldnt need to know what is in the string , he would only specify the part of the string he wanted to remove, and the string he wanted to put in its place. your way is prob better but theres a lot of stuff going on,and i like to keep things simple. both ways would work though
 
Hmm... I was under the impression that hes doing that already since it is an external file. I just dont see how he would get that whole string into a .replace and why he would want to .replace in the first place? He just wants to Remove some text.


Minor edit to the above:
Endpt = text.IndexOf(System.Environment.NewLine, Endpt + 1) Finds the next end of the line after "Joe King".
 
well, i done that exactly before this, but im not trying to convince you or anyone. it worked for me , but you can think it doesnt work if you want
 
Im bored again today....

[VB]
Dim strFileName As String = "C:\File.txt"
Dim strFileData As String = String.Empty
Dim iStart As Integer = 0
Dim iReadLen As Integer = 0

Read the file data
Dim tr As System.IO.TextReader
tr = IO.File.OpenText(strFileName)
strFileData = tr.ReadToEnd
tr.Close()

I like trim
strFileData = strFileData.Trim

You have two line breaks (CrLf) between the header and the body.
Look for them, and set the start position right after them.
iStart = strFileData.IndexOf(vbCrLf & vbCrLf) + 4

This is how much data you want to read after the start position.
iReadLen = strFileData.Length - iStart

Read from position iStart for iReadLen positions.
strFileData = strFileData.Substring(iStart, iReadLen)

Write the file back out.
Dim tw As System.IO.TextWriter
tw = IO.File.CreateText(strFileName)
tw.Write(strFileData)
tw.Flush()
tw.Close()
[VB]
 
Back
Top