Skip to content

Instantly share code, notes, and snippets.

@wateroot
Last active August 29, 2015 14:02
Show Gist options
  • Save wateroot/72e392e088fdb792f67f to your computer and use it in GitHub Desktop.
Save wateroot/72e392e088fdb792f67f to your computer and use it in GitHub Desktop.
ModifyFileTime
int ModifyFileTime(char *szTgtFile, char *szTimeFile)
{
HANDLE hFileOld = NULL, hFileNew = NULL;
FILETIME stCreateTime = {0}, stLastAccessTime = {0}, stLastWriteTime = {0};
const FILETIME *pCreateTime = NULL, *pLastAccessTime = NULL, *pLastWriteTime = NULL;
int nRet = 0;
hFileOld = CreateFile(szTimeFile,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFileOld == INVALID_HANDLE_VALUE) {
return -1;
}
hFileNew = CreateFile(szTgtFile,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFileNew == INVALID_HANDLE_VALUE) {
CloseHandle(hFileOld);
return -1;
}
nRet = GetFileTime(hFileOld, &stCreateTime, &stLastAccessTime, &stLastWriteTime);
if (0 == nRet) { //return nonzero if success.
nRet = -1;
goto __END;
}
pCreateTime = &stCreateTime;
pLastAccessTime = &stLastAccessTime;
pLastWriteTime = &stLastWriteTime;
nRet = SetFileTime(hFileNew, pCreateTime, pLastAccessTime, pLastWriteTime);
nRet = (0 != nRet) ? 0 : (-1); //return nonzero if success.
__END:
CloseHandle(hFileNew);
CloseHandle(hFileOld);
return nRet;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment