Delphi Tutorial: Obtain the correct file size with TWin32FindData.

 

Introduction

 

This short tutorial explains how to use obtain the correct file size from the TsearchRec.FindData structure for files that are larger than 2GB.

 

TSearchRec

 

Delphi lets you use FindFirst and FindNext to obtain information about files. If the file was found, FindFirst returns a zero and places file information into a TSearchRec record which has the following structure:

 

TSearchRec = record

            Time: Integer;                   // time of last modification

            Size: Integer;                   // file size

            Attr: Integer;                   // file attributes

            Name: TFileName;                 // file name

            ExcludeAttr: Integer;

            FindHandle: THandle;

            FindData: TWin32FindData;        // Win32_FIND_DATA structure

end;

 

Size contains the size of the file in bytes – at least that is what it says in Delphi’s help. Unfortunately if the file is greater than 2 Gigabytes, size will yield the incorrect size. The good news is that you can still obtain the correct file size from TSearchRec using TSearchRec.FindData.

 

TsearchRec.FindData corresponds to the WIN32_FIND_DATA structure used in the Win32 FindFirstFile and FindNextFile API functions. It is defined as follows:

 

TWin32FindData structure.

 

type

  PWin32FindDataA = ^TWin32FindDataA;

  PWin32FindDataW = ^TWin32FindDataW;

  PWin32FindData = PWin32FindDataA;

  _WIN32_FIND_DATAA = record

    dwFileAttributes: DWORD;                          // file attributes

    ftCreationTime: TFileTime;                        // Created date and time

    ftLastAccessTime: TFileTime;                      // Last accessed date and time    

    ftLastWriteTime: TFileTime;                       // Modified date and time

    nFileSizeHigh: DWORD;                             // file size high word

    nFileSizeLow: DWORD;                              // file size low word

    dwReserved0: DWORD;                               // reserved for future use

    dwReserved1: DWORD;                               // reserved

    cFileName: array[0..MAX_PATH - 1] of AnsiChar;    // full file name

    cAlternateFileName: array[0..13] of AnsiChar;     // alternate (short) file name

  end;

 

Obtaining the correct file size.

 

The file size in this structure is defined by nFileSizeHigh and nFileSizeLow. You can convert this to an Int64 value using the formula:

 

FileSize =  nFileSizeHigh shl 32 + nFileSizeLow

 

To ensure the correct result, you must explicitly typecast all variables and values in the expression as Int64. The following function uses FindFirst to obtain the exact file size from the FindData structure.

 

// return the exact file size for a file. Return zero if the file is not found.

 

Function FileSize(FileName : String) : Int64;

var

  SearchRec : TSearchRec;

begin

 

  if FindFirst(FileName, faAnyFile, SearchRec ) = 0 then                  // if found

     Result := Int64(SearchRec.FindData.nFileSizeHigh) shl Int64(32) +    // calculate the size

               Int64(SearchREc.FindData.nFileSizeLow)

  else

     Result := 0;

  FindClose(SearchRec);                                                   // close the find

end;

 

 

 

 

 

References

 

References on your computer.

 

Delphi 6 online help

 

See: FindFirst, FindNext, TSearchRec

WIN32.HLP

 

The Microsoft Win32 Programmers Reference

Windows.pas

 

Borland Delphi Run-time Library - Win32 API Interface Unit.  

 

Internet

 

Using FindFirstFile and FindNextFile

Explores the FindFirstFile and FindNextFile Win32 API functions and examines the TWin32FindData record structure in detail.

 

Other tutorials

 

Other tutorials in this series

The Jedi Code Library

 

An excellent cooperative effort among Delphi programmers with lots of free source code

The MER System Database Search

 

A free newsgroup search engine you can use to find Delphi newsgroup postings

Microsoft Developers Network

 

The Microsoft support site for developers

Borland Developers Network

 

Borland’s support site.

Newsgroups

Borland Delphi newsgroups.

 

 

Books

 

The Tomes of Delphi: Win32 Core API - Windows 2000 Edition by John Ayers, ISBN 1-55622-750-7 Wordware Publishing Inc

 

The Tomes of Delphi: Win32 Shell API – Windows 2000 Edition by John Ayers, ISBN 1-55622-749-3 Wordware Publishing Inc

 

Windows 2000 SYSTEMS PROGRAMMING Black Book by Al Williams, 2000, ISBN 1-57610-280-7, The Coriolis Group

 

Misc.

Use of Source Code

The source code contained in this tutorial is provided free of charge, AS IS WITHOUT WARRANTY OF ANY KIND AND IS PROVIDED WITHOUT ANY IMPLIED WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY. You may incorporate this source code into your own program but you do so at your own risk. Please remember to test your program and any source code copied from this site. Please report errors and improvements to jpgriffiths.com.

Copyright

Copyright © 2002 - 2006 Paul Griffiths,  www.jpgriffiths.com.

Last Update

Last Updated:  [May, 2006]

Achknowledgement

Special thanks to Pierre R for correcting an error in the FileSize example.

 

 

Home | Tutorials Home