Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the modification time of a file programmatically?

Tags:

c

file

windows

How do I set the modification time of a file programmatically in Windows?

like image 262
Jay Avatar asked Feb 02 '10 15:02

Jay


3 Answers

From: http://rosettacode.org/wiki/File/Modification_Time#C

#include <time.h>
#include <utime.h>
#include <sys/stat.h>

const char *filename = "input.txt";

int main() {
  struct stat foo;
  time_t mtime;
  struct utimbuf new_times;

  stat(filename, &foo);
  mtime = foo.st_mtime; /* seconds since the epoch */

  new_times.actime = foo.st_atime; /* keep atime unchanged */
  new_times.modtime = time(NULL);    /* set mtime to current time */
  utime(filename, &new_times);

  return 0;
}
like image 69
DVK Avatar answered Oct 12 '22 01:10

DVK


Windows (or the standard CRT, anyhow) has the same utimes family of functions that UNIX has.

struct _utimebuf t;
t.tma = 1265140799;  // party like it's 1999
t.tmm = 1265140799;
_utime(fn, &t);

Using Win32 functions, FILE_BASIC_INFO can be set using SetFileInformationByHandle.

FILE_BASIC_INFO b;
b.CreationTime.QuadPart = 1265140799;
b.LastAccessTime.QuadPart = 1265140799;
b.LastWriteTime.QuadPart = 1265140799;
b.ChangeTime.QuadPart = 1265140799;
b.FileAttributes = GetFileAttributes(fn);
SetFileInformationByHandle(h, FileBasicInfo, &b, sizeof(b));
like image 33
ephemient Avatar answered Oct 12 '22 03:10

ephemient


Use SetFileInformationByHandle with FileInformationType as FILE_BASIC_INFO

like image 29
mmmmmm Avatar answered Oct 12 '22 02:10

mmmmmm