Obtain and change the folder time attribute using VC ++

Source: Internet
Author: User
Tags filetime
For most IT enterprises and most individual users, data backup is mainly a simple backup of data content, this rarely backs up the attributes of backup data, such as the file attribute and the time attribute of the folder. In some special industries that have strict requirements on data management, it is equally important to back up these attributes. However, in the current Windows operating system, it is difficult to completely copy this information, whether it is directly performed through manual data backup or some data backup software, in particular, it is difficult to maintain the original information of time attributes that are changing every moment. In addition, Windows does not even provide tools and means to directly modify such attributes. To this end, I have studied this problem and summarized a simple solution. The author has explained how to back up and change the file attributes in the topic "getting and changing file attributes under VC ++" on the computer report. Therefore, this article will not go into detail here, but will focus on getting and changing the folder time attribute.

Design Concept

In Windows, no methods or methods are provided to modify the folder time attribute, even in Win32 API functions, it only provides function calls for modifying the time attribute of a file, but there is no phrase for modifying the time attribute of a folder. Although the backup program provided by Windows can completely copy the Time Attributes of all subfolders in the folder to be backed up, it cannot keep the root directory time attribute constant. Therefore, we can consider using backup methods and start with Win32 API functions related to backup. Specifically, you can open the folder by opening the file, and then use the Win32 API functions GetFileTime () and SetFileTime () originally used to process the file time attribute () to obtain the original time attribute and set the folder time attribute after backup with its parameter. This process ensures that the folder time attribute is consistent before and after backup.

According to the previous analysis, it can be seen that opening a folder by opening a file is the key to the entire process. It is usually used to create and open a file using the Win32 API function CreateFile () it is not only used to create and open file objects. In fact, it can also be used to create and open pipelines, mail slots, communication resources, and disk drives (for Windows NT only) console and folder (can only be opened. The following is a prototype of CreateFile:

HANDLE CreateFile (LPCTSTR lpFileName, // File Name Pointer
DWORD dwDesiredAccess, // Access Mode
DWORD dw1_mode, // share mode
LPSECURITY_ATTRIBUTES lpSecurityAttributes, // Security Attribute
DWORD dwCreationDisposition, // Creation Method
DWORD dwFlagsAndAttributes, // file attributes
HANDLE hTemplateFile // pointer to the file to be copied
);

When you use it to open a folder, the first parameter lpFileName should be set to the name of the folder to be opened. The access mode can be set as needed. For this article, the source folder can only be read, so it can be set to GENERIC_READ. For the backup folder, because attribute information needs to be written, GENERIC_WRITE must be supported; there is no difference between the setting of the Shared Mode Parameter dw1_mode and the setting during file processing. Here we can set it to file_1__read | file_1__delete; because the CreateFile () function is used for folder operations, it cannot be created. Therefore, the Creation method can only open existing objects, that is, dwCreationDisposition should be set to OPEN_EXISTING. In contrast, it is important to set the dwFlagsAndAttributes parameter, creat is created by setting this parameter to the FILE_FLAG_BACKUP_SEMANTICS attribute. The eFile () function to open a folder.

Generally, the time attribute of files and folders refers to the creation time, recent access time, and recent modification time. The preceding attributes of a file can be obtained through GetFileTime (). For folders, after opening the folder using the CreateFile () function, the obtained handle can be used as a file handle. Therefore, you can use the GetFileTime () function to obtain the time attribute of the folder. The GetFileTime () function prototype is as follows:

BOOL GetFileTime (HANDLE hFile, // file HANDLE
LPFILETIME lpCreationTime, // address of the Creation Time
LPFILETIME lpLastAccessTime, // address of the last access time
LPFILETIME lpLastWriteTime // address of the last modification time
);

The last three parameters all point to the FILETIME structure and get the UTC time. If necessary, you can use the FileTimeToLocalFileTime () function to convert the UTC time to the local time. In addition, you can use the FileTimeToSystemTime () function to convert the file time format to the system time format. The converted time format is saved in a SYSTEMTIME structure object. Similarly, when writing time information to the folder attribute, if it is not the file time format, you should also use the SystemTimeToFileTime () function to convert it from the system time format to the file time format, then, use the SetFileTime () function to write the specified time to the time attribute of the folder. In this way, all folders, including the root directory, can maintain the same time attribute during data backup and recovery.

Simple Example

The following is a simple application example based on the previous descriptions, in this example, the time attribute information can be read from the specified folder and written back after modification (only the last modification time is processed here, other time attributes can be implemented in a similar way ). Here, two functions getdirtime () and setdirtime () are used to obtain and change the folder time information. The following describes the implementation process of these two functions in the form of Annotations:

// Obtain the time attribute of the specified folder. The entry parameter dirname specifies the folder to be processed, and stime is
// Pointer to the systemtime Structure
Bool csetfordertimedlg: getdirtime (cstring dirname, systemtime & stime ){
// Open a folder
Handle hdir = createfile (dirname, generic_read,
File_cmd_read | file_cmd_delete,
Null, open_existing,
File_flag_backup_semantics, null );
Filetime lpcreationtime; // Folder creation time
Filetime lplastaccesstime; // The last access time to the folder
Filetime lplastwritetime; // The last modification time of the folder.
// Obtain the folder time attribute information
If (GetFileTime (hDir, & lpCreationTime, & lpLastAccessTime, & lpLastWriteTime )){
FILETIME ftime;
FileTimeToLocalFileTime (& lpLastWriteTime, & ftime); // The local time for conversion
FileTimeToSystemTime (& ftime, & stime); // convert to system time format
}
CloseHandle (hDir); // close the opened folder
Return retval;
}

// Set the time attribute of the specified folder. The entry parameter DirName specifies the folder to be processed, new_time
// A pointer to the SYSTEMTIME Structure
BOOL CSetForderTimeDlg: SetDirTime (CString DirName, SYSTEMTIME new_stime ){
// Open the Win32 API call of the Directory
HANDLE hDir = CreateFile (DirName, GENERIC_READ | GENERIC_WRITE,
File_cmd_read | file_cmd_delete,
NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS, NULL );
FILETIME lpCreationTime; // Folder creation time
FILETIME lpLastAccessTime; // The last access time to the folder
FILETIME lpLastWriteTime; // The last modification time of the folder.
SystemTimeToFileTime (& new_stime, & lpCreationTime); // convert to the file time format
SystemTimeToFileTime (& new_stime, & lpLastAccessTime );
SystemTimeToFileTime (& new_stime, & lpLastWriteTime );
// Set the time attribute of the folder
BOOL retval = SetFileTime (hDir, & lpCreationTime, & lpLastAccessTime, & lpLastWriteTime );
CloseHandle (hDir); // close the folder
Return retval;
}

At this point, it is convenient to call the GetDirTime () and SetDirTime () functions to obtain and set the time attribute of any specified folder, specifically:

SYSTEMTIME stime; // System Time Structure object
If (GetDirTime (m_Path, stime ))
{
// If the time attribute of the folder is obtained successfully, the obtained time information will be saved in the stime structure object.
......
// If necessary, you can modify the obtained time attribute or keep it unchanged.
......
// Write the modified time attribute back to the folder
SetDirTime (m_Path, stime );
}

Summary

This article uses the CreateFile () function to open a folder and treat it as a file in future processing. Therefore, you can use GetFileTime (), SetFileTime () to obtain and write the time attribute. You can set the time attribute for any folder including the root directory. It has good application prospects in data backup and restoration. The code described in this article is compiled by Microsoft Visual C ++ 2000 under Windows 6.0 Professional.

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.