Re: *.dat - Delete Record
- From: "Randy Birch" <rgb_removethis@xxxxxxxx>
- Date: Mon, 9 Jan 2006 22:58:55 -0500
Yes. Create a new project with three wide command buttons. Paste in the
following, watch for line wrapping, and change the paths in command1/2and3,
and the Delete routine, to point to a valid drive on your system. This demo
uses the SetEndOfFile API after calculating the current size and a new size
based on the UDT being used in the file. The demo creates a random access
file with 15 records. The data for the records looks like:
begin record 1
aaaaa
1234
3445
09 Jan 2006 10:50:43 PM
the end of 1
begin record 2
bbbbb
1234
3445
09 Jan 2006 10:50:43 PM
the end of 2
begin record 3
....
.... and so on through "the end of record 15". I used this format so you can
see that after running the demo and hitting the delete last record button,
the debug.data will show the file ends at record 14.
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2006 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const OPEN_ALWAYS As Long = 4
Private Const FILE_BEGIN As Long = 0
Private Const GENERIC_WRITE As Long = &H40000000
Private Const GENERIC_READ As Long = &H80000000
Private Const FILE_ATTRIBUTE_NORMAL As Long = &H80
Private Type TestData
s1 As String * 15
s2 As String * 5
dw1 As Long
dt1 As Date
dw2 As Long
s3 As String * 13
End Type
Private Declare Function CreateFile Lib "kernel32" _
Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
Private Declare Function GetFileSize Lib "kernel32" _
(ByVal hfile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function SetFilePointer Lib "kernel32" _
(ByVal hfile As Long, _
ByVal lDistanceToMove As Long, _
lpDistanceToMoveHigh As Long, _
ByVal dwMoveMethod As Long) As Long
Private Declare Function SetEndOfFile Lib "kernel32" _
(ByVal hfile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hfile As Long) As Long
Private Sub Form_Load()
Command1.Caption = "Create random file"
Command2.Caption = "Test read random file"
Command3.Caption = "Delete last random record"
End Sub
Private Sub Command1_Click()
Dim td As TestData
Dim nodata As TestData
Dim cnt As Long
Dim hfile As Long
hfile = FreeFile
Open "d:\test.txt" For Random Access Write As #hfile Len = Len(td)
For cnt = 1 To 15
With td
.s1 = "begin record " & cnt
.s2 = String(Len(.s1), cnt + 96)
RSet .s3 = "the end of " & cnt '& String(Len(.s3), cnt + 96)
.dw1 = 1234
.dw2 = 3445
.dt1 = Now
End With
Put #hfile, cnt, td
td = nodata
Next cnt
Close #hfile
End Sub
Private Sub Command2_Click()
Dim td As TestData
Dim nodata As TestData
Dim cnt As Long
Dim hfile As Long
Dim totalrecords As Long
hfile = FreeFile
Open "d:\test.txt" For Random Access Read As #hfile Len = Len(td)
totalrecords = LOF(hfile) \ Len(td)
For cnt = 1 To totalrecords
Get #hfile, cnt, td
With td
Debug.Print .s1
Debug.Print .s2
Debug.Print .dw1
Debug.Print .dw2
Debug.Print .dt1
Debug.Print .s3
End With
td = nodata
Next cnt
Close #hfile
End Sub
Private Sub Command3_Click()
DeleteLastRandomRecord
End Sub
Private Sub DeleteLastRandomRecord()
Dim td As TestData
Dim hfile As Long
Dim totalrecords As Long
Dim newfilesize As Long
Dim sfilename As String
Dim dwFileSizeLow As Long
Dim dwFileSizeHigh As Long
sfilename = "d:\test.txt"
'hwo many records in file?
hfile = FreeFile
Open sfilename For Random Access Read As #hfile Len = Len(td)
totalrecords = LOF(hfile) \ Len(td)
Close #hfile
'can't do it if less than 2
If totalrecords >= 2 Then
'open using API
hfile = CreateFile(sfilename, _
GENERIC_WRITE Or GENERIC_READ, _
0&, ByVal 0&, _
OPEN_ALWAYS, _
FILE_ATTRIBUTE_NORMAL, _
0&)
'get the file size. Note that the value
'returned is the low word size of the
'file; typically for normal files the
'high word will be 0, but if it's not
'then these have to be combined to get
'the actual size. So test dwFileSizeHigh
'before truncating.
dwFileSizeLow = GetFileSize(hfile, dwFileSizeHigh)
If dwFileSizeHigh = 0 Then
'dwFileSizeLow for this test is the file size.
'one record is Len(td), so to delete the last
'record:
newfilesize = dwFileSizeLow - Len(td)
Debug.Print totalrecords, dwFileSizeLow, newfilesize
'so move to the new end position ..
If SetFilePointer(hfile, newfilesize, dwFileSizeHigh, FILE_BEGIN) >
0 Then
'time to delete the last record
Call SetEndOfFile(hfile)
End If 'SetFilePointer
End If 'dwFileSizeHigh
CloseHandle hfile
End If
End Sub
--
Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/
Please reply to the newsgroups so all can participate.
"James" <admin@xxxxxxxxxxxxxx> wrote in message
news:1136767893.625882.307300@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
: If removing middle records (1-8) in this example, is it possible to
: delete the last record (9).
:
: Thanks, James.
:
.
- References:
- *.dat - Delete Record
- From: James
- Re: *.dat - Delete Record
- From: James
- *.dat - Delete Record
- Prev by Date: Re: Combo-Box dynamic event change in VB6
- Next by Date: Re: Printers and Faxes shortcut
- Previous by thread: Re: *.dat - Delete Record
- Next by thread: Re: *.dat - Delete Record
- Index(es):