Getting extended file / folder attributes
- John
- 1 day ago
- 3 min read
This post is one of a series providing implementation examples of Windows API Functions, Types, Enums and Consts using VBA. The code in this post can be used as-is, however, if you regularly (or even just occasionally) work with Windows API declarations in VBA, you may want to see the posts Automatically add Windows API declaration(s) and Using 'F1' to view Windows API web pages which explain some of the functionality that can be added to the VBE by VBE_Extras.
This post follows on from the Getting file / folder attributes post which is introduced as follows ...
Using the GetAttr VBA Function you can get various attributes for a file or folder including whether it is read-only, hidden, a system file and whether it is a file or folder. You can get a little more detail using the Attributes property of the FileSystemObject. But for even more attributes, you can use the GetFileAttributes Windows API Function which will retrieve any of the attributes listed on the File Attributes Constants page ...
Well, you can get even more attributes for a file or folder using the GetFileAttributesEx Windows API Function. In addition to the relevant members of the File Attributes Constants, you also get the file creation date/time, the file last accessed date/time, the file last written date/time and the file size. This information is provided in a WIN32_FILE_ATTRIBUTE_DATA Type (click the link to read more information about the data provided by each member of the Type) that is populated by the GetFileAttributesEx Function.
And here is how you do that ...
As well as the GetFileAttributeEx Windows API Function, there is also:
The FileTimeToSystemTime Function which converts a FILETIME Type to a SYSTEMTIME Type ... we need this because the WIN32_FILE_ATTRIBUTE_DATA Type that is populated by the GetFileAttributeEx Function must be converted to a SYSTEMTIME in order that we can take the time zone into consideration (and so get the correct local times)
The GetTimeZoneInformation Function which populates a TIME_ZONE_INFORMATION Type containing information about the time zone on the device
The SystemTimeToTzSpecificLocalTime Function which takes the SYSTEMTIME from the FileTimeToSystemTime Function which is in UTC (Coordinated Universal Time) and the TIME_ZONE_INFORMATION from the GetTimeZoneInformation Function and converts it into another SYSTEMTIME which is in the local time zone
There are then some VBA helper-Functions ...
ConvertFileTimeToLocalDate ... which coordinates all of the above conversion from a FILETIME to a local SYSTEMTIME and then converts that to a date via ConvertSystemTimeToDate
ConvertSystemTimeToDate ... which converts the local SYSTEMTIME to a Date
GetDoubleFromTwoLongs ... which creates a Double value from the file size which is provided as two Long values
BytesToXB ... which converts the Double value of the file size to something more human-readable in Bytes, Kb, Mb or Gb as appropriate (credit to Mike Wolfe at https://nolongerset.com/64-bit-unsigned-integers-in-vba/)
GetFILE_ATTRIBUTEAsString ... which creates a text String from the file attributes enumeration
The core code is then the TryGetAttributesEx Function ... this is the Function you could call from your own code. It tries to get the attributes for the file and returns a True / False value for success (or not).
So that you can test all of this, the code includes two "testing" Subs ...
LogInfoAboutFile ... sends details to the Immediate window about the attributes for a file so that you can see the results
TestGetFileAttributesEx ... run this to test ... calls LogInfoAboutFile multiple times for various files and folders ... there is no guarantee that all of these exist on your device so please adjust the paths or remove them if desired ... add files and folders of your own!
Here is an example of the output for a specific file on my device ...



Comments