Results 1 to 7 of 7

Thread: [RESOLVED] Delete Multiple Lines From Text File!

  1. #1

    Thread Starter
    Frenzied Member arpan_de's Avatar
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    1,394

    Resolved [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?


    ARPAN

    IF YOU HAVE AN APPLE & I HAVE AN APPLE AND WE EXCHANGE THE APPLES, THEN YOU & I WILL STILL HAVE ONE APPLE BUT IF YOU HAVE AN IDEA & I HAVE AN IDEA AND WE EXCHANGE OUR IDEAS, THEN EACH OF US WILL HAVE TWO IDEAS!

    NOTHING IS IMPOSSIBLE IN THIS WORLD.....EVEN THE WORD IMPOSSIBLE SAYS I'M POSSIBLE!

    PRACTICE MAKES A MAN PERFECT BUT NOBODY IS PERFECT; SO WHY PRACTICE?

  2. #2
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    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

  3. #3

    Thread Starter
    Frenzied Member arpan_de's Avatar
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    1,394

    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.


    ARPAN

    IF YOU HAVE AN APPLE & I HAVE AN APPLE AND WE EXCHANGE THE APPLES, THEN YOU & I WILL STILL HAVE ONE APPLE BUT IF YOU HAVE AN IDEA & I HAVE AN IDEA AND WE EXCHANGE OUR IDEAS, THEN EACH OF US WILL HAVE TWO IDEAS!

    NOTHING IS IMPOSSIBLE IN THIS WORLD.....EVEN THE WORD IMPOSSIBLE SAYS I'M POSSIBLE!

    PRACTICE MAKES A MAN PERFECT BUT NOBODY IS PERFECT; SO WHY PRACTICE?

  4. #4
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,692

    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.

  5. #5

    Thread Starter
    Frenzied Member arpan_de's Avatar
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    1,394

    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.
    What difference will that make? I can do the same in a text file as well........


    ARPAN

    IF YOU HAVE AN APPLE & I HAVE AN APPLE AND WE EXCHANGE THE APPLES, THEN YOU & I WILL STILL HAVE ONE APPLE BUT IF YOU HAVE AN IDEA & I HAVE AN IDEA AND WE EXCHANGE OUR IDEAS, THEN EACH OF US WILL HAVE TWO IDEAS!

    NOTHING IS IMPOSSIBLE IN THIS WORLD.....EVEN THE WORD IMPOSSIBLE SAYS I'M POSSIBLE!

    PRACTICE MAKES A MAN PERFECT BUT NOBODY IS PERFECT; SO WHY PRACTICE?

  6. #6
    Lively Member
    Join Date
    Jun 2005
    Location
    New Jersey, USA
    Posts
    119

    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:
    1. public sub deletefavorite()
    2.  
    3. ' build a variable containing all of the favorites
    4.  
    5. open favoritefile for input as #1
    6. do while not eof(1)
    7.   line input #1, x
    8.   fav = fav & "`" & x
    9. loop
    10. close
    11.  
    12. 'convert the variable into an array
    13.  
    14. array = split(fav, "`")
    15.  
    16. 'clear the file of favorites
    17.  
    18. open favoritefile for output as #1
    19. print #1, ""
    20. close
    21.  
    22. 'the loop rebuilding the file, excluding the deleted favorite
    23.  
    24. for a = lbound(array) to ubound(array)
    25.   if favorite_to_be_deleted = array(a) then goto 1
    26.   open favoritefile for append as #1
    27.   print #1, array(a)
    28.   close
    29. 1 next a
    30. end sub

  7. #7

    Thread Starter
    Frenzied Member arpan_de's Avatar
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    1,394

    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:
    1. Private Sub DeleteIEFavorites()
    2.     Dim i As Integer
    3.     Dim str As String
    4.     Dim Fav As String
    5.     Dim iFile As Integer
    6.     Dim FileName As String
    7.     Dim arrFav() As String
    8.    
    9.     iFile = FreeFile
    10.     FileName = App.Path & "\Favorites.txt"
    11.  
    12.     Open FileName For Input As #iFile
    13.         Do While Not EOF(iFile)
    14.             Line Input #iFile, str
    15.             Fav = Fav & "`" & str
    16.         Loop
    17.     Close
    18.  
    19.     arrFav = Split(Fav, "`")
    20.  
    21.     Open FileName For Output As #iFile
    22.     Close
    23.    
    24.     For i = 0 To UBound(arrFav)
    25.         If (arrFav(i) <> vbNullString And InStrRev(arrFav(i), "^^^^^") = 0) Then
    26.             Open FileName For Append As #iFile
    27.                 Print #iFile, arrFav(i)
    28.             Close #iFile
    29.         End If
    30.     Next i
    31. 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.............


    ARPAN

    IF YOU HAVE AN APPLE & I HAVE AN APPLE AND WE EXCHANGE THE APPLES, THEN YOU & I WILL STILL HAVE ONE APPLE BUT IF YOU HAVE AN IDEA & I HAVE AN IDEA AND WE EXCHANGE OUR IDEAS, THEN EACH OF US WILL HAVE TWO IDEAS!

    NOTHING IS IMPOSSIBLE IN THIS WORLD.....EVEN THE WORD IMPOSSIBLE SAYS I'M POSSIBLE!

    PRACTICE MAKES A MAN PERFECT BUT NOBODY IS PERFECT; SO WHY PRACTICE?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width