Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to change creation date attribute of an existing file? 1

Status
Not open for further replies.

mingis

Programmer
Jan 23, 2002
475
LT
subj.
 
It's a platform-specific operation (no such standard/portable functions). See the snippet:
Code:
#include <windows.h>
bool ChFileTimeCrtd(const char* fname, const SYSTEMTIME* pst)
{
  HANDLE     h;
  FILETIME   ft;

  if (!pst || !SystemTimeToFileTime(pst,&ft))
     return false;
  h=CreateFile(fname,GENERIC_WRITE,FILE_SHARE_WRITE,
               0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
  if (h == INVALID_HANDLE_VALUE)
     return false;

  BOOL rc = SetFileTime(h,&ft,0,0);
  CloseHandle(h);
  return rc != FALSE;
}

int main(int argc, char* argv[])
{
  SYSTEMTIME t;
  t.wYear  = 2001;
  t.wMonth = 1;
  t.wDay   = 1;
  t.wDayOfWeek = 0;
  t.wHour   = 17;
  t.wMinute = 0;
  t.wSecond = 0;
  t.wMilliseconds = 0;
  ChFileTimeCrtd("testfile.txt",&t);
  return 0;
}
See MSDN for full SetFileTime() parameters description.
Adjust ChFileTimeCrtd() interface as you wish.
Remember: it's UTC time. Correct with your timing zone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top