Skip to content

Instantly share code, notes, and snippets.

@stefansundin
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefansundin/1065c9b4cc9eee8fcbce to your computer and use it in GitHub Desktop.
Save stefansundin/1065c9b4cc9eee8fcbce to your computer and use it in GitHub Desktop.
Retrieve and edit file timestamps in Windows.
#include <stdio.h>
#include <windows.h>
// FILETIME is always UTC. Note that Windows itself will display timestamps in your local timezone, e.g. in the file properties dialog.
// Error codes: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx
// i686-w64-mingw32-gcc -o filetime filetime.c
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: filetime <file> [new mtime] ...\n");
fprintf(stderr, " filetime AltDrag.ini 20140102030405\n");
fprintf(stderr, "\n");
fprintf(stderr, "Note: All times are in UTC!\n");
fprintf(stderr, " Date format: YYYYMMDDhhmmss\n");
return 1;
}
HANDLE f = CreateFile(argv[1], FILE_READ_ATTRIBUTES, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (f == INVALID_HANDLE_VALUE) {
fprintf(stderr, "CreateFile() failed on line %d! Error code: %d.\n", __LINE__, GetLastError());
return 2;
}
FILETIME mt;
SYSTEMTIME st;
if (GetFileTime(f,NULL,NULL,&mt) == 0) {
fprintf(stderr, "GetFileTime() failed on line %d! Error code: %d.\n", __LINE__, GetLastError());
return 2;
}
if (FileTimeToSystemTime(&mt,&st) == 0) {
fprintf(stderr, "FileTimeToSystemTime() failed on line %d! Error code: %d.\n", __LINE__, GetLastError());
return 2;
}
printf("file current mtime: %04d%02d%02d%02d%02d%02d\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
printf("touch -d '%04d-%02d-%02d %02d:%02d:%02d UTC' %s\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, argv[1]);
if (argc > 2) {
char txt[10] = "";
// YYYY
strncpy(txt, argv[2]+0, 4);
txt[4] = '\0';
st.wYear = atoi(txt);
// MM
strncpy(txt, argv[2]+4, 2);
txt[2] = '\0';
st.wMonth = atoi(txt);
// DD
strncpy(txt, argv[2]+6, 2);
st.wDay = atoi(txt);
// hh
strncpy(txt, argv[2]+8, 2);
st.wHour = atoi(txt);
// mm
strncpy(txt, argv[2]+10, 2);
st.wMinute = atoi(txt);
// ss
strncpy(txt, argv[2]+12, 2);
st.wSecond = atoi(txt);
printf("file new mtime: %04d%02d%02d%02d%02d%02d\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
printf("touch -d '%04d-%02d-%02d %02d:%02d:%02d UTC' %s\n", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, argv[1]);
f = ReOpenFile(f, FILE_WRITE_ATTRIBUTES, 0, 0);
if (f == INVALID_HANDLE_VALUE) {
fprintf(stderr, "ReOpenFile() failed on line %d! Error code: %d.\n", __LINE__, GetLastError());
return 2;
}
if (SystemTimeToFileTime(&st,&mt) == 0) {
fprintf(stderr, "SystemTimeToFileTime() failed on line %d! Error code: %d.\n", __LINE__, GetLastError());
return 2;
}
if (SetFileTime(f,NULL,NULL,&mt) == 0) {
fprintf(stderr, "SetFileTime() failed on line %d! Error code: %d.\n", __LINE__, GetLastError());
return 2;
}
}
CloseHandle(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment