Skip to content

Latest commit

 

History

History
46 lines (32 loc) · 1.5 KB

changing-a-file-time-to-the-current-time.md

File metadata and controls

46 lines (32 loc) · 1.5 KB
description ms.assetid title ms.topic ms.date
The following example sets the last-write time for a file to the current system time using the SetFileTime function.
b4a70c01-d5ce-47e8-9918-9c9176894240
Changing a File Time to the Current Time
article
05/31/2018

Changing a File Time to the Current Time

The following example sets the last-write time for a file to the current system time using the SetFileTime function.

The NTFS file system stores time values in UTC format, so they are not affected by changes in time zone or daylight saving time. The FAT file system stores time values based on the local time of the computer.

The file must be opened with the CreateFile function using FILE_WRITE_ATTRIBUTES access.

#include <windows.h>

// SetFileToCurrentTime - sets last write time to current system time
// Return value - TRUE if successful, FALSE otherwise
// hFile  - must be a valid file handle

BOOL SetFileToCurrentTime(HANDLE hFile)
{
    FILETIME ft;
    SYSTEMTIME st;
    BOOL f;

    GetSystemTime(&st);              // Gets the current system time
    SystemTimeToFileTime(&st, &ft);  // Converts the current system time to file time format
    f = SetFileTime(hFile,           // Sets last-write time of the file 
        (LPFILETIME) NULL,           // to the converted current system time 
        (LPFILETIME) NULL, 
        &ft);    

    return f;
}