Re: Need Help deleting record from text file



Using a database for this definitely is overkill, but if you
want to use database (as a learning experience) then SQLite seems a good
option. Free, very fast and very simple.

Secondly, if you want a very fast sort of a 1-D string array then this code, which
I got from Olaf Schmidt will do the job. Compile with all fast compiler options:

Option Explicit

'***Into a form
'(native compiled, all options this Sort needs 0.19 sec for 20000
'Random Strings of 200'er length on a PIII 500).
Private Type SAFEARRAY1D
cDims As Integer
fFeatures As Integer
cbElements As Long
cLocks As Long
pvData As Long
cElements As Long
lLbound As Long
End Type

Private Declare Sub BindArray Lib "kernel32" Alias "RtlMoveMemory" _
(pArr() As Any, PSrc&, Optional ByVal cb& = 4)
Private Declare Sub ReleaseArray Lib "kernel32" Alias "RtlMoveMemory" _
(pArr() As Any, _
Optional PSrc& = 0, _
Optional ByVal cb& = 4)

Public Sub QSort1DP(Arr() As String)

'Dim Compare As String
Dim i As Long
Dim j As Long
Dim Lo As Long
Dim Hi As Long
Dim StPtr As Long
Dim V(0) As String
Dim pV() As Long
Dim sapV As SAFEARRAY1D
Dim pArr() As Long
Dim sapArr As SAFEARRAY1D
Dim p As Long
Dim StSize As Long
Dim StLo() As Long
Dim StHi() As Long

StSize = 255
ReDim StLo(StSize)
ReDim StHi(StSize) 'init the stack

On Error Resume Next
'spans a Long-Array (pArr()) over the StringPointers in Arr()
sapArr.cDims = 1
sapArr.cbElements = 4 'Bytes used by each StrPointer
sapArr.pvData = VarPtr(Arr(0))
sapArr.cElements = UBound(Arr) - LBound(Arr) + 1

If Err Then
Err.Clear
Exit Sub 'Arr was not initialized
End If

On Error GoTo 0 'switch off Err-Handler for speed-reasons
BindArray pArr, VarPtr(sapArr)

'another Array, used to hold only one single String,
'respective its pointer for reasons of comparing inside the algo
sapV.cDims = 1
sapV.cbElements = 4
sapV.pvData = VarPtr(V(0))
sapV.cElements = 1
BindArray pV, VarPtr(sapV)

StPtr = 1 'init the StackPointer
StLo(0) = LBound(Arr)
StHi(0) = UBound(Arr)

Do
StPtr = StPtr - 1
Lo = StLo(StPtr)
Hi = StHi(StPtr)
If Hi - Lo < 12 Then 'MinSort
For Lo = Lo To Hi - 1
j = Lo
For i = Lo + 1 To Hi
If Arr(i) < Arr(j) Then j = i
Next i
If j <> Lo Then
p = pArr(j): pArr(j) = pArr(Lo): pArr(Lo) = p
End If
Next Lo
Else 'QSort
Do
i = Lo: j = Hi
pV(0) = pArr((Lo + Hi) \ 2)
Do
Do While Arr(i) < V(0)
i = i + 1
Loop
Do While Arr(j) > V(0)
j = j - 1
Loop
If i <= j Then
p = pArr(i)
pArr(i) = pArr(j)
pArr(j) = p
i = i + 1
j = j - 1
End If
Loop While i <= j

If j - Lo < Hi - i Then
If i < Hi Then
StLo(StPtr) = i
StHi(StPtr) = Hi
StPtr = StPtr + 1
If StPtr >= StSize Then
StSize = StSize + StSize
ReDim Preserve StLo(StSize)
ReDim Preserve StHi(StSize)
End If
End If
Hi = j
Else
If Lo < j Then
StLo(StPtr) = Lo
StHi(StPtr) = j
StPtr = StPtr + 1
If StPtr >= StSize Then
StSize = StSize + StSize
ReDim Preserve StLo(StSize)
ReDim Preserve StHi(StSize)
End If
End If
Lo = i
End If
Loop While Lo < Hi
End If
Loop While StPtr

pV(0) = 0 'don't dispose the current String-Content of V(0)
ReleaseArray pV 'release the Array-Mapping between V() and pV()
ReleaseArray pArr 'relase the Array-Mapping between Arr() and pArr()

End Sub


RBS


"Dave O." <nobody@xxxxxxxxxxx> wrote in message news:e5pT7DRAIHA.4592@xxxxxxxxxxxxxxxxxxxxxxx
Why so defensive? All I said was that for some operations a database is faster than flat files and for some operations it is slower. If you are going to be so dogmatic that you cannot accept that then I suspect that all your opinions are probably questionable.

If you are going to do a binary search of an array then you need the whole thing in memory, or you need to load it in fragments which will be slow and messy.
Of course there is no magic to databases, but you can treat them as black boxes which might as well be magic, the trick is obviously indexing which allows complex queries to be answered far faster than any search on a flat file - again obviously this only refers to large datasets, for a few hundred items a flat file will be faster but we are dealing with sets of several thousand items so the hypothetical examples will be larger sets where the database has the clear advantage.

The OP is as you say reading in the data to an array - He then needs to store it so he can compare the folder content with what is backed up, for that he needs a record of prior content and a record of what's archived which will be either the flat file or the database, so a database will speed it up because he does not have to read in the array of existing content and previous archive records because all that can be left unloaded but accessible in the database.

Also once you have a rough idea of what you are doing coding for databases with VB6 is a doddle, while maintaining ones own index of flat file content and devising a suitable random access method to get at the bits you need is a lot more complex.

Dave O.

"Mike Williams" <mikea@xxxxxxxxxxxxxxxxx> wrote in message news:%23059sMPAIHA.1204@xxxxxxxxxxxxxxxxxxxxxxx
"Dave O." <nobody@xxxxxxxxxxx> wrote in message news:u6TkcoOAIHA.4732@xxxxxxxxxxxxxxxxxxxxxxx

Entering data into a database will be a bit slower
as it indexes the data on entry however retrieval
will be faster.

Faster than what?

The other main speed advantage is that opening a
database should be a lot faster than reading the
whole array into memory.

Who said you need to read an entire array into memory just to access a small portion of it? I certainly never suggested that. If you need to save a very large array to disk as a data file you can use standard VB I/O code to do it efficiently. If you write the data in a suitable format you can later open the data file by reading just a relatively small header chunk and then read in the elements or the portion of data you require. This method will work for all data types, including large variable length strings, if you write the code properly. Obviously the indexing method chosen will vary depending on the circumstances and on the kind of data and the kind of uses you are likely to put it to, but it certainly is possible to do this stuff very effieciently in ordinary VB code without needing to use a standard "database". Databases do not contain "alien magic" ;-)

Besides, the OP is reading his data (the names of the files contained in a specific folder) using FindFirstFile and FindNextFile (presumably) and dumping that data (the names of the files) directly in to a VB string array and then sorting the array. How is using a database going to speed up that operation!

Mike






.



Relevant Pages

  • Re: Sorting a string array
    ... 'Dim Compare As String ... Dim StPtr As Long ... Dim StSize As Long ... I'm needing a routine that can sort a string array containing ...
    (microsoft.public.excel.programming)
  • Re: Array sorting
    ... If you really want very fast performance to sort a 1-D string array then use ... Dim lhigh2 As Long ... Dim StPtr As Long ... Dim StSize As Long ...
    (microsoft.public.excel.programming)
  • Re: Sorting a string array
    ... I was able to re-write my code to use a 0-based array, ... 'Dim Compare As String ... Dim StPtr As Long ... Dim StSize As Long ...
    (microsoft.public.excel.programming)
  • Re: Sorting a string array
    ... I was able to re-write my code to use a 0-based array, ... 'Dim Compare As String ... Dim StPtr As Long ... Dim StSize As Long ...
    (microsoft.public.excel.programming)
  • Fast sort of 1-D 1-based string array
    ... Use this code posted by Olaf Schmidt to sort a 1-D 0-based string array and it is very fast. ... 'Dim Compare As String ... Dim StPtr As Long ... Dim StSize As Long ...
    (microsoft.public.vb.general.discussion)

Loading