Correctly calculates a File size from a WIN32_FIND_DATA structure

Category:
Files and Directories
Type:
Snippets
Difficulty:
Intermediate
Author:
Anonymous

Version Compatibility: Visual Basic 6, Visual Basic 5

More information:
'' 'AUTHOR: Art Araya - Salty Brine Software ' 'PURPOSE: Correctly calculates a File size from a WIN32_FIND_DATA structure's ' FileSizeHigh and FileSizeLow values ' 'BACKGROUND: The standard formula given on the MS site produces incorrect results for large files!!! ' This erroneous solution has been reproduced all over the Internet and is in use in code all over the world! ' ' see this page on the MS site for the erroneous computation: ' http://support.microsoft.com/default.aspx?scid=kb;EN-US;185476 ' ' The standard formula is given as: ' FileSize = (nFileSizeHigh * MAXDWORD) + nFileSizeLow (where MAXDWORD is defined as &HFFFF) ' ' ' There are several problems with this code: ' 1) The MAXDWORD constant is defined as &HFFFF. But the maximum value for a DOUBLE_WORD is ' &HFFFFFFFF. The value given on the MS site is actually the maximum value for a WORD not a DWORD. ' 2) The MS computation fails when the nFileSizeLow is a negative number. ' A negative file size is returned by the MS computation in this situation. This will occur for ' large files that are smaller than 4 GIG in size ' 3) nFileSizeHigh should be multiplied by (MAXDWORD + 1), not by MAXDWORD alone. ' ' The MS code works most of the time however. The reason for this is that for normal sized files, ' nFileSizeHigh will usually be zero and nFileSizeLow will usually be a positive number. ' When nFileSizeHigh is zero and nFileSizeLow is a positive number the equation simplifies ' itself to "FileSize = nFileSizeLow". This simple equation works for most normal file sizes. ' The errors in the computation do not appear until the file sizes get larger. ' ' The routine below corrects all of these problems and will return the correct file size for ' even HUGE files (> 4 GIG). The file size is returned in a Currency datatype since 64 bits are required ' to hold the results of this computation. ' '''''''''''''''''

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Code: