List Info

Thread: Delete specific line from a textfile




Delete specific line from a textfile
country flaguser name
United States
2008-03-05 01:56:09

Good morning,
I was wondering the most efficient way to delete a line from a text
file.
I know the line number that I need to delete. I am currently opening
the file, reading the entire file into an array, (split by vbCrLf),
then write the file back to a file less the one line I wish to remove.

AS the textfiles can have 2000+ lines, I thought this was a bit
inneficient and there must be a better way ?

I would appreciate your comments

__._,_.___
.

__,_._,___
Re: Delete specific line from a textfile
country flaguser name
United States
2008-03-05 07:37:41

I would be doing what you are doing.
However you don't describe what you are doing after the split ?
There may be a more efficient approach than what you are doing.

Do you intend to -
- Delete the line
or
- Leave the line present, but empty it ?

I will assume that you wish to remove the line completely.
(If there were 1000 lines, it will now have 999 lines)

Whilst you have it in the array, replace the text in the target array
element with "zyxwv"
Then do a Join using vbCrLf
Then do a replace of
vbCrLf & "zyxwv" & vbCrLf with ""

That approach allows you to use the efficient Join

HTH,
Rob

__._,_.___
.

__,_._,___
Re: Delete specific line from a textfile
country flaguser name
United States
2008-03-05 12:16:32

something similar to this will do what you want.......

Public Sub SeekAPerson(ByVal index As Integer)
Try
FileOpen(1, "c:people.txt", OpenMode.Random)
Dim onePerson As Record
Seek(1, index)
FileGet(1, onePerson)
FileClose(1)
Console.WriteLine(onePerson.Name & " " & onePerson.ID)
Catch
' Error recovery code here.
End Try
End Sub


testblokepuma < k-addie%40keihin-europe.co.uk">k-addiekeihin-europe.co.uk> wrote: Good morning,
I was wondering the most efficient way to delete a line from a text
file.
I know the line number that I need to delete. I am currently opening
the file, reading the entire file into an array, (split by vbCrLf),
then write the file back to a file less the one line I wish to remove.

AS the textfiles can have 2000+ lines, I thought this was a bit
inneficient and there must be a better way ?

I would appreciate your comments





---------------------------------
Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.

[Non-text portions of this message have been removed]

__._,_.___
.

__,_._,___
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )