Results 1 to 28 of 28

Thread: How to delete data on .txt file in VB6?

  1. #1

    Thread Starter
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Angry How to delete data on .txt file in VB6?

    anybody can help me on how to delete data line in .txt file after i read the data? plz help me out... tnx in advance... THIS IS VB6. sorry i put on network programming
    Last edited by noielen; Dec 19th, 2005 at 08:12 PM.
    noister
    <advertising link removed by moderator>

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to delete data on .txt file in VB6?

    Just write back the lines you want to keep to a new file, delete the old file, and rename the new file to match the old name.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to delete data on .txt file in VB6?

    Moved to Classic VB

  4. #4

    Thread Starter
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Re: How to delete data on .txt file in VB6?

    what do u mean dglienna? i'll transfer the data line on a old file to a new file?

  5. #5

    Thread Starter
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Re: How to delete data on .txt file in VB6?

    because what i want in my project is to read the file in every 5 mins or 30 mins or etc. (depends on options), because that file is always stored new data in every new call. - 1 line is equal to 1 call - so what i want is after i read the new line or new call i delete that line.

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: How to delete data on .txt file in VB6?

    The usual way is to

    • read in the file line by line
    • write all the lines out to a new, temporary, file except for the line(s) you want to delete
    • kill the original file
    • rename the temp file with the name of the old file.

  7. #7
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: How to delete data on .txt file in VB6?

    would a database be more useful?

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to delete data on .txt file in VB6?

    If you just want to keep new calls in a different file, read them in from the first file, and append them to the old file, and delete the new file. That way, the old fille would contain all the new entries, while the new one (using append if there is more than one possible call in the interval) would contain new calls since last read.

    It's easier than I thought. I thought you wanted to delete just one line from a file, when, in fact, you just want to append them to the old file. Have the call center write to the new/temp file, and your main app append the contents to the new file.

    This assumes that the call center doesn't have to edit the original information before it gets recorded (but they could still get it from the old file). You'd have to not add those to the new/temp file, or you'd have duplicates.

  9. #9

    Thread Starter
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Re: How to delete data on .txt file in VB6?

    actually after i read the line and before i delete it, i store that line into access db. coz now has many calls, and if i didn't delete the old lines in a file, my program will load slow coz my code is to read all the lines and check if that is new call(s).

    and access db now contains all old and new data lines or calls.
    so what i want to know now is how to delete a (particular) line in a file? tnx...
    Last edited by noielen; Dec 19th, 2005 at 09:09 PM.

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to delete data on .txt file in VB6?

    If you store the Ticket number in the DB, you could run a SQL query for it, and if it isn't there, add it, but if it is, then allow them to edit/update the information.
    That doesn't sound too difficult. Set up the table in Access, and use VB to insert/update it.

  11. #11

    Thread Starter
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Re: How to delete data on .txt file in VB6?

    any help here? plz continue...

  12. #12
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: How to delete data on .txt file in VB6?

    You could just load the file into a listbox, then remove the items you don't need, then save it back into the file

  13. #13

    Thread Starter
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Re: How to delete data on .txt file in VB6?

    Again! HOW TO DELETE A PARTICULAR LINE IN a FILE? (.txt file)

  14. #14
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: How to delete data on .txt file in VB6?

    Sorry
    For an easy question, no one is providing much help eh?

    This will remove line 4 from the text file called txtfile.txt, Hope it helps

    VB Code:
    1. Private Sub Form_Load()
    2. Dim strdata As String
    3. linenum = 4 'the line you wish to remove
    4. Open App.Path & "\txtfile.txt" For Input As #1
    5. For x = 1 To linenum - 1
    6.     If EOF(1) Then Exit Sub
    7.     Input #1, txt
    8.     strdata = strdata & vbCrLf & txt
    9. Next
    10.     If EOF(1) Then Exit Sub
    11.     Input #1, txt
    12. While Not EOF(1)
    13.     Input #1, txt
    14.     strdata = strdata & vbCrLf & txt
    15. Wend
    16. Close #1
    17.  
    18. Open App.Path & "\txtfile.txt" For Output As #1
    19.     Print #1, Mid(strdata, 2, Len(strdata) - 1)
    20. Close #1
    21.    
    22. End Sub

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to delete data on .txt file in VB6?

    That will erase the last line, but I'm not clear on exactly which line you want to delete, and why you wouldn't add all records to one file using append (which writes to the end of the file) and then start fresh with a new file?

  16. #16
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: How to delete data on .txt file in VB6?

    Quote Originally Posted by dglienna
    That will erase the last line

    It worked with me, the first loop is to move to just before the line, then i read the line (but i don't add it to the final string) then it moves through the rest of the file. Finally it writes the file.

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to delete data on .txt file in VB6?

    OK. I didn't look at it closely, but I think he'd want to search for a string to delete. Who knows the actual line number of a record to delete? Nice attempt at answering anyways.

  18. #18
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: How to delete data on .txt file in VB6?

    Thanks

  19. #19
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: How to delete data on .txt file in VB6?

    VB Code:
    1. Private Sub Form_Load()
    2. Dim strdata As String
    3. strFindstring = "4"
    4. Open App.Path & "\txtfile.txt" For Input As #1
    5. While Not EOF(1)
    6.     Input #1, txt
    7.     strdata = strdata & vbCrLf & txt
    8. Wend
    9. Close #1
    10.  
    11. Replace strdata, strFindstring & vbCrLf, vbNullString
    12.  
    13. Open App.Path & "\txtfile.txt" For Output As #1
    14.     Print #1, Mid(strdata, 2, Len(strdata) - 1)
    15. Close #1
    16.    
    17. End Sub

  20. #20
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to delete data on .txt file in VB6?

    This updates the tkt number every half second, but doesn't append the file, and clear the old one for 5 ticks. It displays the method that I was talking about. There is only up to 5 numbers in the newfile, and a list of them all in the oldfile.
    VB Code:
    1. Option Explicit
    2.  
    3. Dim tkt As Long
    4. Dim ff As Integer
    5.  
    6. Private Sub Form_Load()
    7.   Timer1.Interval = 500
    8.   Timer1.Enabled = True
    9.   tkt = 1
    10. End Sub
    11.  
    12. Private Sub Timer1_Timer()
    13.   Static ct As Long
    14.   ct = ct + 1
    15.   tkt = tkt + 1
    16.   ff = FreeFile
    17.   Open "G:\oldfile.txt" For Append As #ff
    18.     Print #ff, tkt
    19.   Close #ff
    20.   If ct Mod 5 = 0 Then
    21.     Call savefile
    22.   End If
    23. End Sub
    24.  
    25. Sub savefile()
    26.   Dim str As String
    27.   ff = FreeFile
    28.   Open "G:\oldfile.txt" For Input As #ff
    29.     str = Input(LOF(ff), ff)
    30.   Close #ff
    31.   str = Left(str, Len(str) - 2) ' trim last linefeed
    32.   ff = FreeFile
    33.   Open "G:\newfile.txt" For Append As #ff
    34.     Print #ff, str
    35.   Close #ff
    36.   Kill "g:\oldfile.txt"
    37. End Sub

  21. #21

    Thread Starter
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Re: How to delete data on .txt file in VB6?

    how about, after read all datas on that file, clean it or erase all the datas. how to do it?

  22. #22
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: How to delete data on .txt file in VB6?

    Do you mean make a new file that is empty or remove it completly (delete it)?

    If its to make a new file but empty then use
    VB Code:
    1. Open App.Path & "\txtfile.txt" For Output As #1
    2. Close #1

    But if its for deleting the file use
    VB Code:
    1. Kill App.Path & "\txtfile.txt"

  23. #23

    Thread Starter
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Re: How to delete data on .txt file in VB6?

    all lines on that file. erase it! not the "file" to delete but ALL lines under that file.

  24. #24
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: How to delete data on .txt file in VB6?

    When you delete it and create another one then it won't contain anything as you wanted to...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  25. #25
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How to delete data on .txt file in VB6?

    Step thru my app. You have to change the file locations, unless you have a G: drive. It saves all tkt's in the newfile, but only up to 5 in the oldfile. Then it erases the old file, all in the timer event.

  26. #26

    Thread Starter
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Re: How to delete data on .txt file in VB6?

    dglienna, nothings happen!

  27. #27

    Thread Starter
    Fanatic Member noielen's Avatar
    Join Date
    Nov 2005
    Location
    Cebu, Phil.
    Posts
    680

    Re: How to delete data on .txt file in VB6?

    add more, how to get the no. of lines in a file? hehehe

  28. #28
    Fanatic Member lerroux's Avatar
    Join Date
    Nov 2005
    Location
    Welcome to the darkside... we have cookies
    Posts
    646

    Re: How to delete data on .txt file in VB6?

    oh for god's sakes... manually delete it yourself! lol
    peace

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