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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text File Manipulation

Status
Not open for further replies.
Jan 14, 2003
7
0
0
US
Understand this is a 34 megabyte text file. It came from a unix server.
This is the code I am trying to run. As I write this the program continues to run and has been about 5 minutes.
I am trying to find a certain string in the file which would indicate that the line should be entirely deleted if the string exists in the line.

Let me know if I made any errors ok?

One other thing, when creating additional parameters to remove could you show an example just using "your query here" after the first If End IF
executed...IN other words how do I add additional queries to this to check for?

How do you remove blank lines using this method?

Does it matter the order you do them in?

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button5.Click

Dim strFile As String 'Name of the file

Dim strLine As String 'Data read

Dim textread As IO.TextReader 'Read data of file

Dim strOutputFileContents As String 'Accumulated content for the
output(File)

Dim strDealNumber As String = "***ITEM"

Dim pos As String

strFile =
IO.Path.Combine(IO.Path.GetDirectoryName(Application.ExecutablePath),
"C:/JJK0661.txt")

textread = IO.File.OpenText(strFile)

strLine = textread.ReadLine()

Do While IsNothing(strLine) = False

pos = InStr(strLine, strDealNumber)

If pos Then

strLine = Replace(strLine, strDealNumber, "")

End If

strOutputFileContents = strOutputFileContents + strLine + vbCrLf

Loop

Dim textwrite As IO.TextWriter 'Write data of file

textwrite = IO.File.CreateText(strFile)

textwrite.Write(strOutputFileContents)

textwrite.Close()

End Sub
 
I've not tried out your code, but I think you've got a logic error

If pos Then

strLine = Replace(strLine, strDealNumber, "")

End If

strOutputFileContents = strOutputFileContents + strLine + vbCrLf

This will replace all occurences of "***ITEM" with an empty string. At the top of your post, you said that you wanted to delete the entire line, so you might try replacing this section with

If not pos Then strOutputFileContents = strOutputFileContents + strLine + vbCrLf

Then it should work in principle.

However, there are a few things you could do to speed it up.

You don't need to read it one line at a time - if you've got enough available RAM, then read the entire file into memory (into a string)

Use regular expressions to replace all occurences of a line containing your string with nothing.

Write out the string to a new file.

I've done stuff like this with ~ 10 MB text files and it is pretty quick.

Hope this helps - give a shout if you've any questions.


PS - it is better with these things to try the routine on a small file (small enough to see in Notepad) to make sure your idea works in principle.

Mark [openup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top