SetFileTime

 

Description

This function sets the date and time that a file was created, last accessed, or last modified.

Syntax

BOOL SetFileTime( HANDLE hFile, const FILETIME * lpCreationTime, const FILETIME * lpLastAccessTime, const FILETIME * lpLastWriteTime )

Parameters

hFile
[input] Handle to the file for which to set the dates and times. The file handle must have been created with GENERIC_WRITE access to the file.
lpCreationTime
[input] Pointer to a FILETIME structure that contains the date and time the file was created. This parameter can be NULL if the application does not need to set this information.
lpLastAccessTime
[input] Pointer to a FILETIME structure that contains the date and time the file was last accessed. The last access time includes the last time the file was written to, read from, or (in the case of executable files) run. This parameter can be NULL if the application does not need to set this information.
lpLastWriteTime
[input] Pointer to a FILETIME structure that contains the date and time the file was last written to. This parameter can be NULL if the application does not want to set this information.

Return

TRUE if success, FALSE if error

Examples

EX1

int SetFileTime_ex1()
{
    char* strFilename = "c:\\test.txt";
    BOOL bRet = FALSE;
    HANDLE hFile = CreateFile(strFilename, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile != INVALID_HANDLE_VALUE)
    {
        FILETIME ft;
        SYSTEMTIME st;
        GetSystemTime(&st);              // gets current time
        SystemTimeToFileTime(&st, &ft);  // converts to file time format
        bRet = SetFileTime(hFile,NULL, NULL, &ft);
        CloseHandle(hFile);
    }
    return 1;
}

Remark

See Also

SystemTimeToFileTime

header to Include

origin.h

Reference