[RESOLVED] Delete Multiple Lines From Text File!
How do I delete multiple lines from a text file?
Actually in my web browser project, users can add different URLs to their list of Favorites. The Favorites are written in a text file as & when they get added. Like IE, the Favorites are displayed in a menu. Users can also import their IE Favorites for which they have to click a menu item. When IE Favorites get imported, those Favorites are also populated in the text file where the other Favorites are written.
Now I want to give users the option to delete IE Favorites, if one wishes to do so. Deleting IE Favorites means all the IE Favorites have to be deleted from the text file & at the same time, the Favorites that the user added exclusively should be retained! So how do I delete all the IE Favorites from the text file ensuring that the non-IE Favorites are retained?
Re: Delete Multiple Lines From Text File!
Why not store them in two different text files. For example "Non IE Favorites.txt" and "IE favorites.txt".
Jenova
Re: Delete Multiple Lines From Text File!
That can be done but why create an extra text file, isn't it?
What I intend is since it will be difficult to distinguish between IE & non-IE Favorites, when all the IE Favorites get populated in the text file, I will append some text like "^^^^^" (without the quotes) at the very end of the IE Favorites so that VB can easily differentiate between the IE & non-IE Favorites. Finally when deleting the IE Favorites, I will tell VB to delete only those Favorites (each Favorite comes on a new line) from the text file that have ^^^^^ at the very end.
Re: Delete Multiple Lines From Text File!
Why does it have to be a text file? Save it as a data file with url and "IsFromIE" fields.
Re: Delete Multiple Lines From Text File!
Quote:
Why does it have to be a text file? Save it as a data file with url and "IsFromIE" fields.
What difference will that make? I can do the same in a text file as well........
Re: Delete Multiple Lines From Text File!
try this:
step 1: save each favorite into an array
step 2: rewrite the text file line by line, comparing each line to the string representing the deleted favorite
step 3: if the favorite from the array is equal to the deleted favorite, skip to the next variable in the array.
step 4: when the loop is complete, your new favorites file will not have the deleted favorite
step 5: update the menu
here is the code:
VB Code:
public sub deletefavorite()
' build a variable containing all of the favorites
open favoritefile for input as #1
do while not eof(1)
line input #1, x
fav = fav & "`" & x
loop
close
'convert the variable into an array
array = split(fav, "`")
'clear the file of favorites
open favoritefile for output as #1
print #1, ""
close
'the loop rebuilding the file, excluding the deleted favorite
for a = lbound(array) to ubound(array)
if favorite_to_be_deleted = array(a) then goto 1
open favoritefile for append as #1
print #1, array(a)
close
1 next a
end sub
Re: Delete Multiple Lines From Text File!
Thanks, mate, you have given me a great suggestion. This is what I did finally (each of the IE Favorites, when added to the text file, were appended with ^^^^^ at the very end):
VB Code:
Private Sub DeleteIEFavorites()
Dim i As Integer
Dim str As String
Dim Fav As String
Dim iFile As Integer
Dim FileName As String
Dim arrFav() As String
iFile = FreeFile
FileName = App.Path & "\Favorites.txt"
Open FileName For Input As #iFile
Do While Not EOF(iFile)
Line Input #iFile, str
Fav = Fav & "`" & str
Loop
Close
arrFav = Split(Fav, "`")
Open FileName For Output As #iFile
Close
For i = 0 To UBound(arrFav)
If (arrFav(i) <> vbNullString And InStrRev(arrFav(i), "^^^^^") = 0) Then
Open FileName For Append As #iFile
Print #iFile, arrFav(i)
Close #iFile
End If
Next i
End Sub
Now the IE Favorites are getting deleted & at the same time, the Favorites exclusively added by the user remain in the text file.
I must once again confess that you really gave me a fantastic idea :)
Thanks once again.............