Solved

Delete records from a text file

Posted on 2002-04-18
14
172 Views
Last Modified: 2010-05-02
I have a text file that writes records 65 characters across then carriage returns and starts a new record.

I need to read that text file and delete an entire record, if it does not meet a verification check.

How do I do this without rewriting the entire file?

Because these records could be in the middle, I don't want to have to read and recreate every text file excluding the records that don't meet the verification.

Is there a way just to delete that record from the text file without rewriting the whole text file to not include the record?  

Thanks...

0
Comment
Question by:KimberlyMinarik
  • 4
  • 2
  • 2
  • +4
14 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6950943
you can open it in a richtextbox do your edits there and then save it back.
0
 

Author Comment

by:KimberlyMinarik
ID: 6950954
How do you do that???
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 6951060
try something like this:

put richtextbox control on form and call it richtextbox1

Private Sub Command1_Click()
    Dim records() As String

    RichTextBox1.LoadFile "C:\my documents\test.txt"
   
    records = Split(RichTextBox1.Text, vbCrLf)

    For i = 0 To UBound(records)
        If records(i) <> "Hello World" Then '' you would put your checks here
            RichTextBox1.Find records(i) & vbCrLf, 1, Len(RichTextBox1.Text)
            RichTextBox1.SetFocus
            SendKeys "{DELETE}", True
        End If
    Next

    RichTextBox1.SaveFile "C:\my documents\test.txt", rtfText
End Sub

test.txt looks like this before:
Hello World
name1
name2
Hello World
name3
name4

and after:
Hello World
Hello World

0
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 6951208
Hi,

Doing the rich text box is writing the whole file back to the disk. There is no way to delete part of a file from the middle. The only thing you can do is rewrite the entire file, using whatever method, or have a flag for each record saying whether it is valid or not.

Zaphod.
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6951547
You could try creating a text-based file DSN (data source) and then using standard data access techniques to handle the records - that would probably improve your whole code.

See (I know its RDO but the idea, and indeed the code, is virtually identical)
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q187670

0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6951548
You could try creating a text-based file DSN (data source) and then using standard data access techniques to handle the records - that would probably improve your whole code.

See (I know its RDO but the idea, and indeed the code, is virtually identical)
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q187670

0
 
LVL 1

Accepted Solution

by:
olx earned 100 total points
ID: 6951708
there is no need to erase it, just ignore it.

put a single char flag at the beginning of the record, when the record is invalid, mark it as 0 or whatever you like.  When the record is valid, mark it as 1.
process only the ones with a "valid record" mark.

No need to erase them.

i also have an app to resize easily fixed lenght files.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6952040
olx, that's a good idea but as records are added, the file size grows up and the performance would suffers.
Besides, you need to rewrite entire file to put the flag, am i right?
0
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 6952198
No, you don't need to rewrite the entire file to change the value of certain bytes in the middle, you just have to open it in binary mode then write to the appropriate bytes in the file. You just cannot add or remove bytes from the middle without rewriting the entire file, or at least the entire file after that point.

Zaphod.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6952239
Overwrite the record with the contents of the last record, and use SetEndOfFile to make your file shorter.
0
 
LVL 1

Expert Comment

by:olx
ID: 6954033
Kimberly:


- my original idea was to make the change (add the flag) in the file structure definition and start capturing "from now on" with the flag.  no to rewrite te whole db each time.

- if the discarded records are less than 5% of the total records you get in a period.. then no problem.

- i was thinking last night that if you do the check while you receive de information then you could discard the record inmediately.  capturing and checking are made by different apps at different times?
if not, thats it, you could receive de record, checkit and if its ok, write it, if not, dont.

*** Other option is to capture in a file, (same structure, just named diferent, acording to date) and when you make the check use that file, good records go to the main file, bad ones get ignored.  and at the end, delete the file.   To avoid receiving in the same file that you are checking, you could capture in files named by the date. 20020418_Buffer.dat

Hope this helps

:)


0
 
LVL 1

Expert Comment

by:olx
ID: 6954076
if you need to capture and check in realtime then you cant use the file buffer.

BUT then what comes next is to mark the record as discarded and when you add other record, doit over the discarded records.  

here is some code.

'put this in a module
type isamB01 as records
   Exist as string *1
   ClientCode as long
   Reference as date
   Amount as currency
   EndOfRecord as string *3
end type

'put this to get a new record

Private Sub cmdNew_Click()

    Dim b01 As isamB01
    Dim r01 As Long
    Open file For Random As #1 Len = Len(b01)
    r01 = 0
    Do
        r01 = r01 + 1
        Get #1, r01, b01
        If b01.Exist <> "1" Then  ' good record
       
        b01.Amount = x
        b01.ClientCode = y
        b01.EndOfRecord = ">" & vbCrLf
        b01.Exist = "1"
        b01.Reference = Now()
       
        Put #1, r01, b01
    Loop
       
    Close 1
   
End Sub


'put this to discard records.
Private Sub cmdDiscard_Click()

    Dim b01 As isamB01
    Dim r01 As Long
    Open file For Random As #1 Len = Len(b01)
    r01 = x 'record# to be discarded
    Get #1, r01, b01
    b01.Exist = "X"
    Put #1, r01, b01
    Close 1
   
End Sub

hope this helps
0
 
LVL 1

Expert Comment

by:olx
ID: 6954097
probably you think that is sooooo slow to re read the whole db each time, but it takes less than a second to read a 10,000 records of 65char lenght.

and at the moment of creating a new record you could start from half the file.

instead of

r01 = 0

use

r01 = (lof(1) / 65) /2
'the filelen, in records, cut in half

this way you assure you could reuse the last discarded records.

:)
0
 

Author Comment

by:KimberlyMinarik
ID: 6994734
Sorry I took so long to add.  I have been away do to family issues.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now