Can you load the entire file into memory?
If so, load the whole thing
- skip or eliminate the data you don't want
- rewrite the file.
thines01
Postaholic
2,425 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
Sure (technique 1):
Imports System.IO
Imports System.Linq
Module Module1
Public Function DeleteTopTwoLines(ByVal strFileName As String, ByRef strError As String) As Boolean
Dim blnRetVal As Boolean = True
Try
Dim strData() As String = File.ReadAllLines(strFileName)
File.WriteAllLines(strFileName, strData.ToList().Skip(2).ToArray())
Catch ex As Exception
blnRetVal = False
strError = ex.Message
End Try
Return blnRetVal
End Function
Sub Main()
Dim strError As String = ""
If (Not (DeleteTopTwoLines("c:\science\Text1.txt", strError))) Then
System.Diagnostics.Debug.WriteLine("Could not process file: " + strError)
Return
End If
End Sub
End Module
thines01
Postaholic
2,425 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
Which statement?
Did you add the Imports?
thines01
Postaholic
2,425 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
Yes, the function should be INSIDE the class.
thines01
Postaholic
2,425 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
You can use it wherever you want.
I think routines like that need to be kept separate so they can wiork without being bound to the display or a particular piece of code.
All you need to pass to it are the parameters.
I might even add in a new parameter that is the number of lines to delete from the top.
It can also be modified to eliminate lines that contain particular data pieces.
There are a LOT of options.
thines01
Postaholic
2,425 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
... no matter where I look I can't find a thread on any site (not just daniweb) that explains how to do this in visual basic and not vb.net or vbscript....
Basically what I'm trying to do here is take a file that is made in another portion of the program and delete the first two lines out of it. From what I can tell everything else works, including the portion that copies the data from one file to the other. I just don't know how to get rid of those two lines in the first file.
Any help would be much appreciated. Thank you. -Zoidmaster
First of all, the technique is the same no matter what language you use.
Granted, I didn't read all these posts because the solution is soooo simple, and you claim you have "the portion that copies the data from one file to the other" working, so the solution is
Ready...?
Read the first two lines of the file.
Now copy the rest of the file with the portion that works.
By reading the first to lines, they are no longer available to be read so will be gone from the copied file.
IOW, read the file; write only the lines you want copied, don't write the rest.
WaltP
Posting Sage w/ dash of thyme
10,507 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944