Modify the file creation date, access date, and modification date

Source: Internet
Author: User
Tags filetime

Sometimes, for some synchronization or other messy reasons, we need to modify the file creation date and other content. I found an algorithm on the Internet and wrote it down for future use.

Void cmodifybindlg: onbutton1 ()
{
// Todo: add your control notification handler code here

// Define a structure compatible with the file information to save the obtained information
Typedef struct _ file_info {
Tchar szfiletitle [128]; // file title name
DWORD dwfileattributes; // file attributes
Filetime ftcreationtime; // File Creation Time
Filetime ftlastaccesstime; // last Object Access time
Filetime ftlastwritetime; // The last modification time of the object
DWORD nfilesizehigh; // high dual-character file size
DWORD nfilesizelow; // low dual-Text of the file size
DWORD dwreserved0; // reserved, 0
DWORD dwreserved1; // reserved, 0
} File_info, * pfile_info;

// Obtain the object information

Win32_find_data FFD;
Handle hfind = findfirstfile (m_filename, & FfD );

Findclose (hfind );

// Save the file information
File_info fileinfo;
Memset (& fileinfo, 0, sizeof (file_info ));
// Save the file property information to fileinfo for backup
Strcpy (fileinfo. szfiletitle, FFD. cfilename );
Fileinfo. dwfileattributes = FfD. dwfileattributes;
Fileinfo. ftcreationtime = FfD. ftcreationtime;
Fileinfo. ftlastaccesstime = FfD. ftlastaccesstime;
Fileinfo. ftlastwritetime = FfD. ftlastwritetime;
Fileinfo. nfilesizehigh = FfD. nfilesizehigh;
Fileinfo. nfilesizelow = FfD. nfilesizelow;
// Modify the file generation date
Systemtime time;
Filetime t_ft;
Filetimetosystemtime (& (FfD. ftcreationtime), & time );
Time. wyear = 2000;
Time. wmonth = 1;
Time. wday = 1;
Time. wdayofweek = 6;
Systemtimetofiletime (& time, & t_ft );
// Write the modified date back to the file
Fileinfo. ftcreationtime = t_ft;
Bool bresult;
Cfile file (m_filename, cfile: modewrite | cfile: typebinary); // open the file
Bresult = setfiletime (handle) file. m_hfile, // file handle to be written
& Fileinfo. ftcreationtime, // File Creation Time
& Fileinfo. ftlastaccesstime, // last file access time
& Fileinfo. ftlastwritetime); // last file modification time
If (! Bresult)
{
DWORD dwerr = getlasterror ();
}
 
// Setfileattributes ();
File. Close (); // close the file

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.