How can I use C to change the last modified date of a file on Windows?

Is there a C function call that can change the last modified date of a file or directory in Windows?

+1


source to share


3 answers


You can use the SetFileTime function , for directories you have to use CreateFile with the FILE_FLAG_BACKUP_SEMANTICS flag to get the directory handle and use it as the SetFileTime file handle parameter like this:



hFolder = CreateFile(path, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_DIRECTORY | FILE_FLAG_BACKUP_SEMANTICS, NULL);

      

+5


source


Use SetFileTime:

BOOL WINAPI SetFileTime(
  __in      HANDLE hFile,
  __in_opt  const FILETIME *lpCreationTime,
  __in_opt  const FILETIME *lpLastAccessTime,
  __in_opt  const FILETIME *lpLastWriteTime
);

      



Its in winbase.h, so you just need to include windows.h

EDIT: I inserted the wrong function.

+2


source


Yes. You can use API function SetFileTime .

+1


source







All Articles