Retrieving the Last-Write Time

The following example retrieves the last-write time for a file, converts it to local time based on the current time-zone settings, and creates a date and time string that can be shown to the user.

// GetLastWriteTime - retrieves the last-write time and converts the
// time to a string
// Return value - TRUE if successful, FALSE otherwise
// hFile - must be a valid file handle
// lpszString - address of buffer to receive string

BOOL GetLastWriteTime(HANDLE hFile, LPSTR lpszString)
{
FILETIME ftCreate, ftAccess, ftWrite, ftLocal;
SYSTEMTIME stCreate;

// Retrieve the file times for the file.
if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
return FALSE;

// Convert the last-write time to local time.
if (!FileTimeToLocalFileTime(&ftWrite, &ftLocal))
return FALSE;

// Convert the local file time from UTC to system time.
FileTimeToSystemTime(&ftLocal, &stCreate);

// Build a string showing the date and time.
wsprintf(lpszString, "%02d/%02d/%d %02d:%02d",
stCreate.wDay, stCreate.wMonth, stCreate.wYear,
stCreate.wHour, stCreate.wMinute);

return TRUE;
}