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... :ehh: THIS IS VB6. sorry i put on network programming
Printable View
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... :ehh: THIS IS VB6. sorry i put on network programming
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.
Moved to Classic VB
what do u mean dglienna? i'll transfer the data line on a old file to a new file?
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.
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.
would a database be more useful?
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.
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...
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.
any help here? plz continue...
You could just load the file into a listbox, then remove the items you don't need, then save it back into the file
Again! HOW TO DELETE A PARTICULAR LINE IN a FILE? (.txt file)
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:
Private Sub Form_Load() Dim strdata As String linenum = 4 'the line you wish to remove Open App.Path & "\txtfile.txt" For Input As #1 For x = 1 To linenum - 1 If EOF(1) Then Exit Sub Input #1, txt strdata = strdata & vbCrLf & txt Next If EOF(1) Then Exit Sub Input #1, txt While Not EOF(1) Input #1, txt strdata = strdata & vbCrLf & txt Wend Close #1 Open App.Path & "\txtfile.txt" For Output As #1 Print #1, Mid(strdata, 2, Len(strdata) - 1) Close #1 End Sub
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?
:ehh:Quote:
Originally Posted by dglienna
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.
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.
Thanks
VB Code:
Private Sub Form_Load() Dim strdata As String strFindstring = "4" Open App.Path & "\txtfile.txt" For Input As #1 While Not EOF(1) Input #1, txt strdata = strdata & vbCrLf & txt Wend Close #1 Replace strdata, strFindstring & vbCrLf, vbNullString Open App.Path & "\txtfile.txt" For Output As #1 Print #1, Mid(strdata, 2, Len(strdata) - 1) Close #1 End Sub
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:
Option Explicit Dim tkt As Long Dim ff As Integer Private Sub Form_Load() Timer1.Interval = 500 Timer1.Enabled = True tkt = 1 End Sub Private Sub Timer1_Timer() Static ct As Long ct = ct + 1 tkt = tkt + 1 ff = FreeFile Open "G:\oldfile.txt" For Append As #ff Print #ff, tkt Close #ff If ct Mod 5 = 0 Then Call savefile End If End Sub Sub savefile() Dim str As String ff = FreeFile Open "G:\oldfile.txt" For Input As #ff str = Input(LOF(ff), ff) Close #ff str = Left(str, Len(str) - 2) ' trim last linefeed ff = FreeFile Open "G:\newfile.txt" For Append As #ff Print #ff, str Close #ff Kill "g:\oldfile.txt" End Sub
how about, after read all datas on that file, clean it or erase all the datas. how to do it?
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:
Open App.Path & "\txtfile.txt" For Output As #1 Close #1
But if its for deleting the file use
VB Code:
Kill App.Path & "\txtfile.txt"
all lines on that file. erase it! not the "file" to delete but ALL lines under that file.
When you delete it and create another one then it won't contain anything as you wanted to... :)
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.
dglienna, nothings happen!
add more, how to get the no. of lines in a file? hehehe
oh for god's sakes... manually delete it yourself! lol
peace