Text Line delete

I Just wondered if anybody had a idea of how to delete a line of text from a text file…?
without creating a new file and copying all lines except the one to be deleted.

Would use SQLite normally but is not my file and cannot change it over to SQlit.

Thanks.

If the file can be loaded into memory, you can do that, delete the line, then overwrite the contents of the file.

If it can’t, the safest way is what you described.

Have managed to input over 10000 lines of text into memory and dupmp them back to the file so thank you very much.
Never thought about doing it that way think my brain is going.

Thank again Kem

Have just noticed that when I close the folderitem it adds a line onto the file but nothing shows in the file but can delete the line after the last line output it seems it putting carriage returns or something anybody any idea…?
Have looked at the file using hex editor and the last to hex numbers are 0D 0A I believe these ar carraige return and linefeed.l
anybody got a way to stop them being output.

Use Write, not WriteLine.

Write just give a block of text and need to read in as readline.

Thanks

Kem suggested to read the entire file into memory, not line be line. How are you doing it? It should be

  • read the entire file (with .ReadAll)
  • split it into an array of lines
  • remove the line you want
  • join it back into a string
  • write that string back out with .Write

At no point should you be using ReadLine or WriteLine.

Thanks will give it a try but is late here 00:14 so will get back to it tomorrow.

Unfortunately, FolderItem.Length is Read Only…

Else, setting it to -1 (or -2) will strip the End Of Line character… :face_exhaling:

:ukraine:

HEY !

WHAT ABOUT: BinaryStream.Length ?

Read the last two characters of the file, check if they are NL+CR or only CR (and the order of appearance) and remove 1 or 2 to the Length property…

No need to load / save the whole file.

Thanks for all the help and info have now done what I needed to do.

Read Whole File
Split using array.Split(CHr(10))
loop thru and ignore line not required.
Put text back to one block
remove file
create file
write back to file.
Close File

Thanks again all who took part…

That’s not the subject of the issue, though. The OP wants to remove an entire line.

Oh !

I’m so tired… I read something else (how to remove the last “empty” line)…

Sorry OP.

Well, you answered for another issue mentioned in the thread; that’s still good :wink:

Just to say the Length Property is READ only.