How can I delete a line from a textfile via code?
Printable View
How can I delete a line from a textfile via code?
The simplest way is using Replace function by replace those line with "" (Nothing) :p
OR
open a new file by copy the first half (from begining till the starting point of the line you which to replace) and follow by copy the second half (from the end of the line which you wish to remove till the end of the file).
regards,
Say I found the word "BOB" in the fifth line of my textfile. Now I want to get rid of that line. How would I do that using the replace function? Would I have to seek to that line or something?
You need to open the file, read all the lines into an array or something, find what you want to remove, remove it from the array, and then write the contents of the array back to the file.
inthis case, i suggest to use the Regular Expression :) Check out the sample at RegExp
VB Code:
Dim tmp as string Dim temp () as string temp () = Open "c:\windows\system\" & sDirname For Input As #1 Do Until EOF(1) Line Input #1, tmp Loop Close #1
I'm kind of confused on this. How would I go about searching in an array for a string? Once I found it, I would just remove the # assigned to it in the array right?
whatz do you wish to search from the "c:\windows\system\" & sDirname?
I just posted a piece of the code. That sDirname is a string that h as the folder name stored that it gets from a dirlistbox. Hows this:
Open "c:\windows\system\modernrock.txt" for input as #1
VB Code:
dim a() as string open "C:\something.txt" for input as #1 a = split(Input$(LOF(1),#1),vbcrlf) 'reads the whole file and splits lines to an array close #1 open "C:\something.txt" for output as #1 for i = lbound(a) to ubound(a) if a(i) <> "BOB" then 'do your searching here print #1,a(i) 'if the line is not BOB then put it in the file (if it IS bob, then it doesn't end if next close #1