Deleting lines in a txt file


Posted by Conor on November 01, 2001 4:53 AM

good day,

I have tried to record a macro to do this but it is not poss as it does not do it the way i want..

Does anyone have code which opens, deletes the first 5 lines and closes/saves a text file??

thank-you
Conor

Posted by Jerid on November 01, 2001 6:01 AM

I had to think out of the box on this one.

Conor, this should work for you. It's the only way I could think of doing it since there isn't a method to delete a line from a text file.

It actually reads all lines except the first 5 and writes them to a new file then delete the old file and rename the new file. Hope it helps.

Sub DeleteLines()
Dim sData As String
Dim sOrgFile As String
Dim sDestFile As String
Dim iCounter As Integer

'Assign File Names
sOrgFile = Application.GetOpenFilename
sDestFile = "C:\Tmp_Holding_File.txt"
iCounter = 0

'Open Files
Open sOrgFile For Input As #1
Open sDestFile For Output As #2

'Start
Do While Not EOF(1)
iCounter = iCounter + 1
Line Input #1, sData

If iCounter > 5 Then
Print #2, sData
End If

sData = ""
Loop

Close #1
Close #2

Kill sOrgFile
FileCopy sDestFile, sOrgFile
Kill sDestFile
End Sub

Jerid



Posted by conor on November 01, 2001 6:20 AM

nice one jarid

'Assign File Names sOrgFile = Application.GetOpenFilename sDestFile = "C:\Tmp_Holding_File.txt" iCounter = 0 Open sOrgFile For Input As #1 Open sDestFile For Output As #2 'Start Do While Not EOF(1) iCounter = iCounter + 1 Line Input #1, sData If iCounter > 5 Then Print #2, sData End If sData = "" Loop Close #1 Close #2 FileCopy sDestFile, sOrgFile Kill sDestFile