views:

302

answers:

3

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

+1  A: 

Yes. You can use the SetFileTime API function.

Rob Kennedy
+2  A: 

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.

FlySwat
+3  A: 

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);
CMS