VC ++: How to get the time and date of a file?

How to get file size and file date stamp in Windows in C ++, given its path?

+3
source share
4 answers

You can use FindFirstFile()to get both of them at once, without having to open it (which is required GetFileSize()and GetInformationByHandle()). This is a bit time consuming, however, so a small wrapper is useful.

bool get_file_information(LPCTSTR path, WIN32_FIND_DATA* data)
{
  HANDLE h = FindFirstFile(path, &data);
  if(INVALID_HANDLE_VALUE != h) {
    return false;
  } else {
    FindClose(h);
    return true;
  }
}

Then the file size is available in nFileSizeHighboth the WIN32_FIND_DATAnFileSizeLow members and the timestamps are available in , and .ftCreationTimeftLastAccessTimeftLastWriteTime

+3
source

GetFileSize/GetFileSizeEx GetFileInformationByHandleEx FileBasicInfo .

, CreateFile .

// Error handling removed for brevity
HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ,
                 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

LARGE_INTEGER fileSize;
GetFileSizeEx(hFile, &fileSize);

FILE_BASIC_INFO fileInfo);
GetFileInformationByHandle(hFile, FileBasicInfo, fileInfo, sizeof(fileInfo));

// fileInfo.CreationTime is when file was created.
+3

POSIX stat, . Windows .

+3

, GetFileTime, . API , , , API GetFileInformationByHandle. BTW GetFileInformationByHandleEx VISTA .

+3

Source: https://habr.com/ru/post/1711383/


All Articles