Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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


2 Answers

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

hFolder = CreateFile(path, GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_DIRECTORY | FILE_FLAG_BACKUP_SEMANTICS, NULL);
like image 122
Christian C. Salvadó Avatar answered Nov 21 '25 13:11

Christian C. Salvadó


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 pasted the wrong function.

like image 21
FlySwat Avatar answered Nov 21 '25 15:11

FlySwat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!