Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I query "Size on disk" file information?

I want to reproduce behaviour exhibited in the Windows Explorer -> Properties dialog -> General property page for any given file. Specifically I want to reproduce the exact value of the "Size on disk" field.

like image 949
OnTheFly Avatar asked Mar 28 '12 08:03

OnTheFly


1 Answers

As others have said, you need to use GetFileInformationByHandleEx, but it looks like you need to use FILE_STANDARD_INFO or FILE_ID_BOTH_DIR_INFO. The information you want is returned in the AllocationSize member of each, but the second is for directory handles, to list files within instead of the directory itself (note: not recursive, just top-level). To make it easier, FILE_STANDARD_INFO has a Directory boolean, so call it first if you're not sure. According to the documentation for FILE_ID_BOTH_DIR_INFO,

AllocationSize Contains the value that specifies how much space is allocated for the file, in bytes. This value is usually a multiple of the sector or cluster size of the underlying physical device.

This seems to give you the Size on Disk information.

I haven't found a Delphi translation of the FILE_ID_BOTH_DIR_INFO structure. The difficulty would seem to be the final member, WCHAR FileName[1], which is described as:

FileName[1]
Contains the first character of the file name string. This is followed in memory by the remainder of the string.

I'm not sure how this would be handled in Delphi.

like image 59
Ken White Avatar answered Sep 25 '22 16:09

Ken White