VC programming changes the attributes of a specified file or folder

Source: Internet
Author: User
Tags filetime
     
   
 
VC programming changes the attributes of a specified file or folder <from the evil baboons>

Files are the most common storage forms of data on disks and programming objects that are frequently used in programming, many programs, especially data transmission and processing applications, need to frequently create, read, and write files. For programs that do not have strict requirements, we usually only care about whether the file content is correct, whether the file size is increased or decreased, or whether the file name meets the requirements. The above elements can obviously meet the actual needs of most programs. However, for some software systems with strict requirements in some special industries, the above elements alone are far from enough, it is often necessary to extract, process, and reset all attributes of a file, such as the file creation time, the last file access time, the last file modification time, file read/write, and file hiding.. For example, modifying the read/write properties of a file can protect and control the file, especially modifying the file generation and access time, it can avoid any inconvenience caused by file modification time disorder caused by anti-virus changes to the system time. In dos, you can use the DOS command to modify the file attributes. In Windows, you can right-click the attributes to change the read/write attributes of a group of files and folders, however, the file generation and access time cannot be modified, and the properties of files and folders in subdirectories cannot be modified.

Based on Windows APIs, this instance designs a general modification method for file and folder attributes (including file generation modification and access time, and read/write hiding, and implements its Visual C ++ program encoding. After the program runs, click the "modify attributes" button to modify the attributes of the "2.doc" document in the project folder to be consistent with those of the" 1.doc" document.
I. Implementation Method

There are a total of nine types of file attributes: file title name and file attributes (read-only, archived, hidden, etc), file creation time, file last access time, file last modification time, file size high dual-word, file size low dual-word, retain, retain. Here, only the file title name and file length can be easily obtained through the cfile class, and there is no way to get and set other attributes.

The API function group in Windows provides many API functions for file read/write and attribute setting, such:

Handle createfile (maid, DWORD, DWORD, lpsecurity_attributes, DWORD, DWORD, handle );

This function generates or opens a system object and returns a handle to access the object. These objects can be files, folders, pipelines, and so on.

Handle findfirstfile (lpctstr, lpwin32_find_data );

This function searches for files and folders that match the specified file name in the specified directory and returns a query handle.

Bool findnextfile (handle, lpwin32_find_data );

This function continues the Query Process specified by the query handle.

Bool findclose (handle );

This function closes the specified query handle and ends the specified query.

DWORD getfileattributes (lpctstr );

This function checks the attribute information of the specified file.

Bool setfileattributes (lpctstr, DWORD );

This function sets the attribute information of the specified file.

The API function group in Windows provides many API functions related to time conversion, such:

Bool systemtimetofiletime (const systemtime *, lpfiletime );

This function converts the system time to the file time. The file time is a 64-bit length, indicating the time offset from January 1, January 1, 1601, in the unit of one thousandth second.

Bool localfiletimetofiletime (const filetime *, lpfiletime );

This function converts the file time of the current time zone to the file time of the Greenwich Mean Time.

Bool setfiletime (handle, const filetime *);

This function sets the time for file generation modification and access. Modifying the file name is simpler. You can directly set the file name parameters in the createfile () or cfile class member function open when creating the file.

In the above function, a Data Structure win32_find_data member variable used by findfirstfile () and findnextfile () contains all the above file attributes, therefore, you can use this structure to obtain and change file attributes. The structure is as follows:

Typedef struct _ win32_find_data {
DWORD dwfileattributes; // file attributes
Filetime ftcreationtime; // File Creation Time
Filetime ftlastaccesstime; // last file access time
Filetime ftlastwritetime; // last modification time of the file
DWORD nfilesizehigh; // The file length is 32 characters long.
DWORD nfilesizelow; // The file length is 32 characters low
DWORD dwreserved0; // reserved by the System
DWORD dwreserved1; // reserved by the System
Tchar cfilename [max_path]; // long file name
Tchar calternatefilename [14]; // file name in the 8.3 format
} Win32_find_data, * pwin32_find_data;

You can use the findfirstfile () function to find the file based on the current file storage path and read the relevant properties of the file to be operated to the win32_find_data structure:

Win32_find_data FFD;

Handle hfind = findfirstfile ("C:/test. dat", & FfD );

When using this structure, you cannot manually modify any data in this structure. For developers, it can only serve as one read-only data, and all its member variables will be filled in by the system. You can find more detailed descriptions about the win32_find_data structure in the msdn help.

To better save the obtained file property information, a custom file_info data structure is constructed for the file property. The obtained property information can be saved here:

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;

First, use the findfirstfile () function to get the file attribute to the findfiledata structure object of win32_find_data. Then, you can use findclose () to close it, copy the file attributes in findfiledata to the structure object fileinfo in the Custom structure file_info for backup. The following are some of the key code for this description:

// Declare the structure object
File_info fileinfo;
Win32_find_data findfiledata;
......
// Obtain the file Property Information
Findclose (findfirstfile ("test.txt", & findfiledata ));
Memset (& fileinfo, 0, sizeof (file_info ));
......
// Save the file property information to fileinfo for backup
Strcpy (fileinfo. szfiletitle, myfile. getfiletitle ());
Fileinfo. dwfileattributes = findfiledata. dwfileattributes;
Fileinfo. ftcreationtime = findfiledata. ftcreationtime;
Fileinfo. ftlastaccesstime = findfiledata. ftlastaccesstime;
Fileinfo. ftlastwritetime = findfiledata. ftlastwritetime;
Fileinfo. nfilesizehigh = findfiledata. nfilesizehigh;
Fileinfo. nfilesizelow = findfiledata. nfilesizelow;
......

After obtaining the original attribute information of a file, you can either re-write the attribute to the file intact or modify one or more attributes before writing the file, to change the file attributes. For example, you can use the setfiletime () function to set the file creation time, last access time, and last modification time:

Setfiletime (handle) destfile. m_hfile, // file handle to be written
& Fileinfo. ftcreationtime, // File Creation Time
& Fileinfo. ftlastaccesstime, // last file access time
& Fileinfo. ftlastwritetime); // last file modification time

You can also use the setfileattributes () function to modify the file attributes:

Setfileattributes (fileinfo. szfiletitle, fileinfo. dwfileattributes );

Ii. programming steps

1. Start visual c ++ 6.0 and generate a dialog box-based application named "File ";

2. On the modify dialog box interface, place a button control on it, set caption to "modify attributes", and then use Class Wizard to add a message response function for the button;

3. Place the "1.doc" and" 2.doc" files in the project folder;

4. Add code and compile and run the program.

3. program code

/////////////////////////////////////
Void cfiledlg: ontest ()
{
Myfile. Open ("1.doc", cfile: modereadwrite );
Destfile. Open ("2.doc", cfile: modereadwrite );
// Declare the structure object
File_info fileinfo;
Win32_find_data findfiledata;
// Obtain the file Property Information
Findclose (findfirstfile ("1.doc", & findfiledata ));
Memset (& fileinfo, 0, sizeof (file_info ));
// Save the file property information to fileinfo for backup
Strcpy (fileinfo. szfiletitle, myfile. getfiletitle ());
Fileinfo. dwfileattributes = findfiledata. dwfileattributes;
Fileinfo. ftcreationtime = findfiledata. ftcreationtime;
Fileinfo. ftlastaccesstime = findfiledata. ftlastaccesstime;
Fileinfo. ftlastwritetime = findfiledata. ftlastwritetime;
Fileinfo. nfilesizehigh = findfiledata. nfilesizehigh;
Fileinfo. nfilesizelow = findfiledata. nfilesizelow;
Setfiletime (handle) destfile. m_hfile, // file handle to be written
& Fileinfo. ftcreationtime, // File Creation Time
& Fileinfo. ftlastaccesstime, // last file access time
& Fileinfo. ftlastwritetime); // last file modification time
Setfileattributes (fileinfo. szfiletitle, fileinfo. dwfileattributes );
Myfile. Close ();
Destfile. Close ();
}

Iv. Summary

In this article, the win32_find_data structure, setfiletime (), setfileattributes () and other major functions are used to obtain and modify the attribute information of disk files. This technology can be used to completely transmit all information (including file content, file name, and file attributes) of files or folders in fields that require strict communication.

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.