Dave's Brain

Browse - Programming Tips - How can I increment the modification timestamp on a file by one second?

Date: 2015sep27
Language: C/C++
OS: Windows

Q.  How can I increment the modification timestamp on a file by one second?

A.

typedef union { FILETIME filetime; ULONGLONG ull; } FileTimeUnion; static ULONGLONG ONE_SECOND = 1000000000 / 100; // FILETIME is 100 nanosecond units BOOL IncrementModTime(LPCSTR szFilename) { HANDLE hFile; FileTimeUnion modtime; SYSTEMTIME st; hFile = CreateFile(szFilename // Name of the file , GENERIC_WRITE // Access mode , FILE_SHARE_WRITE // Share mode , NULL // Security , OPEN_EXISTING // Creation Disposition (OPEN_EXISTING will fail if it doesn't exist - which is what we want) , FILE_ATTRIBUTE_NORMAL // File attributes , NULL); // Template if (hFile == INVALID_HANDLE_VALUE) { printf("Could not open %s for write\n", szFilename); return FALSE; } if (!GetFileTime(hFile, NULL, NULL, &modtime.filetime)) { printf("Could not get the file time on %s\n", szFilename); CloseHandle(hFile); return FALSE; } modtime.ull += ONE_SECOND; if (!SetFileTime(hFile, NULL, NULL, &modtime.filetime)) { printf("Could not set the file time on %s\n", szFilename); CloseHandle(hFile); return FALSE; } CloseHandle(hFile); return TRUE; }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.