PDA

Click to See Complete Forum and Search --> : Some wild calculations to get a pourcentage


Birth
Feb 26th, 2004, 12:53 AM
This is a file that I compressed with my own algorithm (omacmodi.cpr) and need to read back (like a zip or something). Every line have a different lengh and I need to know how many entries are in there so I can display the pourcentage on a progress bar.

Not only I can't find a better way to do this but I find that the calculation for "FloodPercent" is a little bit suspicious...

It works, but is there a better way?

Open App.Path & "\omacmodi.cpr" For Input As #1

' Count the number of records (or entries).
While Not EOF(1)
Input #1, A$: NB = NB + 1
Wend

Seek #1, 1 ' Back to beginning of file

While Not EOF(1)

Input #1, A$: Rec = Rec + 1

If 100 - Int(((100 / NB * (NB - Rec)))) <> LastRec Then
Frm_Modi.Pnl_Status.FloodPercent = 100 - Int(((100 / NB * (NB - Rec))))
End If

LastRec = 100 - Int(((100 / NB * (NB - Rec))))

' If I don't do this, the form is not refreshed. That's another thing!
openforms = DoEvents

If Len(A$) <> 3 Then

MODE$ = Left$(A$, 3)

For I = 4 To Len(A$) - 5 Step 5

code$ = Mid$(A$, I + 1, 5)

TB_OM.AddNew
TB_OM("Code") = code$
TB_OM("Mode") = MODE$
TB_OM.Update

Next

End If

Wend

It took me about a day to build this like 5 years ago. It's the pourcentage that was a *****!

Can anyone make this better or is it just a piece of art like it is? (ya)

Thank you.

PS: There's only 5 hits for "Some wild calculations" on Google

si_the_geek
Feb 26th, 2004, 07:11 AM
Here's a few improvements:

'declare your variables!
Dim iFileNo As Integer
Dim iFileLen As Long
Dim iLengthSoFar As Long
Dim A$
Dim I As Integer
Dim iCurrentPercent As Integer
Dim iLastPercent As Integer
Dim Rec

iFileNo = FreeFile 'Use FreeFile, not a specific number!
Open App.Path & "\omacmodi.cpr" For Input As #iFileNo

iFileLen = LOF(iFileNo) 'Get length of file
iLengthSoFar = 0 'Store amount read so far

While Not EOF(iFileNo)

Input #iFileNo, A$
iLengthSoFar = iLengthSoFar + Len(A$) + 1 'nb: +1 may not be right, it may be +2

'to get percentage use: min% + ((counter/max_count)*(max% - min%))
'assuming you want 0 to 100, that's: 0+(counter/max_count)*(100-0)
iCurrentPercent = (iLengthSoFar / iFileLen) * 100
If iLastPercent <> iCurrentPercent Then
Frm_Modi.Pnl_Status.FloodPercent = iCurrentPercent
End If
iLastPercent = iCurrentPercent

'openforms = 'no need for an assignment, DoEvents can be run on its own
DoEvents

If Len(A$) <> 3 Then

MODE$ = Left$(A$, 3)

For I = 4 To Len(A$) - 5 Step 5

code$ = Mid$(A$, I + 1, 5)

TB_OM.AddNew
TB_OM("Code") = code$
TB_OM("Mode") = MODE$
TB_OM.Update

Next I

End If

Wend

Close #iFileNo

Birth
Mar 2nd, 2007, 08:15 AM
Well hello there!

After all those years I needed that calculation again and I think that it is time
to thank you for your help and the attention you took to answer me.

It works very well.

Thank you very much si_the_geek!

si_the_geek
Mar 2nd, 2007, 11:21 AM
No problem.. I'm surprised it's still useful to you after all this time! :D

TOQ4
Mar 7th, 2007, 01:22 PM
' For sequential files LOC returns position divided by 128
if (LOF(fh) \ 128)<>0 then Progress = LOC(fh) / (LOF(fh) \ 128)

Not sure if it should read / or \... you could try.