Recent

Author Topic: What's wrong with my file size calculations?  (Read 6796 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
What's wrong with my file size calculations?
« on: June 22, 2011, 10:49:09 pm »
Can anyone see what I am doing wrong?

My program finds all files in a chosen directory and "does stuff" with them via a couple of procedures. The relevant code is below, less bits that are not relevant to my question. In brief, as my program goes along through a directory, it calculates the filesize of each file in one of the procedures and renders it as bytes, Kb, Mb or Gb.

Code: Pascal  [Select][+][-]
  1.  
  2. procedure TForm1.MyProcedure(FileIterator: TFileIterator);
  3. var
  4.   SizeOfFile, TotalBytesRead : int64;
  5. ...
  6. begin
  7.   ...
  8.   SizeOfFile := 0;
  9.   ...
  10.  
  11.   if StopScan = FALSE then                     // If Stop button clicked, cancel scan
  12.     begin
  13.     NameOfFileToHash := (FileIterator.FileName);
  14.     SizeOfFile := FileSize(NameofFileToHash);
  15.  
  16.       // Hash the files, using appropriately sized buffers.
  17.  
  18.       if SizeOfFile > 1048576 then  // if > 1Mb
  19.         begin
  20.          fileHashValue := SHA1Print(SHA1File(NameOfFileToHash, 2097152)); //2Mb buffer
  21.         end
  22.       else
  23.         fileHashValue := SHA1Print(SHA1File(NameOfFileToHash));         //1024 byte buffer
  24.       ...
  25.  
  26.       TotalBytesRead := TotalBytesRead + SizeOfFile;  // Remember the total bytes read so far and add to it for each file examined
  27.       edtTotalBytesExamined.Caption := FormatByteSize(TotalBytesRead);
  28.       Application.ProcessMessages;
  29. ...
  30.     end;
  31.     SG.Free;
  32.     FI.Free;
  33. end;                                    
  34.  


The FormatByteSize function is from http://stackoverflow.com/questions/1285979/delphi-function-to-display-number-of-bytes-as-windows-does.

My question si this : why does my value displayed in edtTotalBytesExamined.Caption start and not deviate much from 130Mb? No matter how much data is in my directory, it starts at 130Mb, flickers to about 135Mb, then back to 130Mb etc.

Ted
« Last Edit: June 22, 2011, 10:51:00 pm by tedsmith »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: What's wrong with my file size calculations?
« Reply #1 on: June 22, 2011, 11:07:46 pm »
Hi,

1) where is variable TotalBytesRead initialized?
2) what is FormatByteSize; it does not seem to be fpc/Lazarus function (or maybe only I don't know it)
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: What's wrong with my file size calculations?
« Reply #2 on: June 22, 2011, 11:34:46 pm »
Quote
1) where is variable TotalBytesRead initialized?

Well, it wasn't as such. The procedure is called every time a file is found from the previous ButtonOnClick event. At the end of each file, the TotalBytesRead is kept full and then added to next time the procedure is called when the next file is found. When there are no further files, it should total the amount read.

However, reading your message made me realise that was the problem, so I made the variable a public one, initialised it at the start of the previous procedure to 0, and now it works :-)

Thanks for the pointer.

Ted

 

Shebuka

  • Sr. Member
  • ****
  • Posts: 427
Re: What's wrong with my file size calculations?
« Reply #3 on: June 23, 2011, 03:12:57 pm »
I don't know why but Mac OS X use telecommunication file size formatting (dividing by 1000 and not 1024), look at the screen...

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: What's wrong with my file size calculations?
« Reply #4 on: June 23, 2011, 03:46:34 pm »
@ (dividing by 1000 and not 1024)

This is invited by hard-disk manufacturers. Their disk seems bigger and they have more money  ;) . My 320GB disk from WD is "only" 298GB.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

 

TinyPortal © 2005-2018