Implement the properties of the specified file and folder with Visual C ++

zhaozj2021-02-17  52

Summary

:

The attributes of the file include the modification and access time of the file, and reading and writing.

DOS

The properties of the next file can be used

DOS

Command is modified,

Windows

Under, you can change a set of files and folders by right-click, but you cannot modify the generated modification and access time of the file, and you cannot modify the files and folders in the subdirectory. This article is based on

Windows

of

API

Designed a universal file and folder attributes (including file generation modification and access time, and reading and writing, etc.), and implemented it

Visual C

Program code.

---- Modification of the properties of the file has a wide range of use value, modifying the read / write edge of the file, can implement the protection and control of the file, especially the modification of the file, modification and access time, can avoid anti-virus change system The inconvenience of the disorder caused by the file modification time, for example, when compiling programmers, the system may recompile documents that modify time disorders, resulting in unnecessary trouble.

---- An API function applied

---- 1, the file function group in the API provides a number of API functions for file read and write, attribute settings, and the functions in this function group used in this encoding are:

Handle Createfile (LPCTSTSTSTER, DWORD, DWORD, LPSECURITY_ATTRIBUTES,

DWORD, DWORD, HANDLE;

---- This function generates or opens a system object and returns a handle to access the object, which can be a file, folder, pipe, etc.

Handle FindfirstFile (LPCTSTR, LPWIN32_FIND_DATA);

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

Bool FindNextFile (Handle, LPWIN32_FIND_DATA);

This function continues a query process specified by the query handle.

Bool FindClose (Handle);

This function closes the specified query handle to end 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.

---- 2, the time function group in the API provides a number of API functions for time conversion, and the functions in the function group used in this encoding are:

Bool SystemTimeTofiletime (const systemtime *, lpfiletime);

This function converts the system time into file time, the file time is a 64-bit length number,

Represents the time offset from January 1, 1601, one million seconds.

Bool LocalFileTimetOfiletime (const filetime *, lpfiletime);

This function converts the file time of this time zone into the file time of Greenwich Time.

Bool SetFileTime (Handle, Const Filetime *, Const filetime *);

This function setting file generates the time of modification and access.

---- II, coding implementation

---- 1, first establish a dialog-based project, place the following control in the dialog form and add appropriate variables to these controls through class management wizard:

File Name Editing box, enter the full path to the file you want to change, can have wildcard * and? . Variable type cstring, name m_filename;

Modify the checkbox, variable type Bool, name m_directory;

Modify file access time check box, variable type bool, name m_access; file access time day edit box, variable type BOOL, name m_aday;

File Access Time Moon Editing Box, Variable Type BOOL, Name M_AMONTH;

File Access Time Annual Editing Box, Variable Type Bool, Name M_AYEAR; Modify File Generation Time Check Box, Variable Type BOOL, Name M_CREATE

File generation time day edit box, variable type BOOL, name m_cday;

The file generates time edit box, variable type BOOL, name m_chour;

File generation time division editing box, variable type BOOL, name m_cminute;

File generation time month edit box, variable type BOOL, name m_cmonth;

File generation time second edit box, variable type BOOL, name m_csecond;

Document generation time year edit box, variable type BOOL, name m_cyear;

Modify file modification time check box, variable type BOOL, name m_modify;

Document modification time day edit box, variable type BOOL, name m_mday;

Edit box, variable type BOOL, name m_mHour

File modification time division editing box, variable type BOOL, name m_mminute;

File modification time month edit box, variable type BOOL, name m_mmonth;

File modification time second edit box, variable type BOOL, name m_msecond;

Document modification time year edit box, variable type BOOL, name m_myear;

Modify the file attribute check box, variable type BOOL, name m_property;

File Hide Properties Check Box, Variable Type BOOL, Name M_Hide;

File read-only property check box, variable type BOOL, name m_read;

File Archive Properties Check Box, Variable Type BOOL, Name M_Save;

File System Properties Check Box, Variable Type BOOL, Name M_System;

Execute the change button, click the button to perform the selected changes.

The above member variables can be initialized with the current system time in the constructor of the dialogue.

---- 2, add the following members in the dialogue:

Char filter [16];

INT ChangetheProperty (lpstr filename, byte bflag = 0);

The member variable filter is used to store file names with wildcard.

The member function ChangeTheProperty implements changes in the properties of the band-all-in-band file, considering the passage of the Miography,

This function uses recursive calls, and please pay attention to the parameter BFLAG, its default value is 0,

1 Represents the cancellation of the call. It is achieved as follows:

INT cchangepropertydlg :: Change

Theproperty (LPSTR FileName, Byte Bflag)

{

Storage path

CHAR PATHNAME [MAX_PATH];

Store full path file name

CHAR AllName [MAX_PATH];

Initialization Pathname

INT Num = Strlen (filename);

For (INT i = NUM-1; I> = 0; i -) {

IF (filename [i] == '//')

Break;

}

IF (i == - 1)

Return 0;

STRNCPY (Pathname, FileName, I 1); Pathname [i 1] = '/ 0';

Find and modify all the qualified files under all subdires

DWORD stat = 0;

Win32_find_data fileinfo;

IF (bflag) {

Char dfilename [MAX_PATH];

STRCPY (DFILENAME, PATHNAME);

STRCAT (DFILENAME, "*. *");

Handle Handle = FindfirstFile

DFileName, & fileInfo;

IF (Handle == Invalid_Handle_Value)

Return 0;

Do {

IF (fileInfo.cfilename [0] == '.')

CONTINUE;

STRCPY (AllName, Pathname);

Strcat (allname, fileinfo.cfilename);

// Check file properties, you can use

FileInfo DWFileAttributes member

Stat = getFileAttributes (allname);

IF (stat == 0xfffffff) {

MessageBox ("Warning, get an error when getting the file information !!",

AllName, MB_OK | MB_ICONInformation;

CONTINUE;

}

// If the query is a folder

IF (Stat & File_Attribute_

Directory && m_directory) {

CHAR TEMP [MAX_PATH];

Sprintf (Temp, "% s% s //% s",

Pathname,

FileInfo.cfilename (Filter);

// Recursively change the file in the subdirectory

ChangetheProperty (TEMP, 1);

}

WHILE (FindNextFile

(Handle, & FileInfo);

FindClose (Handle);

}

Find and modify the eligible files and folders

Handle Handle = FindfirstFile

Filename, & fileInfo;

IF (Handle == Invalid_Handle_Value)

Return 0;

Do {

IF (fileInfo.cfilename [0] == '.')

CONTINUE;

STRCPY (AllName, Pathname);

Strcat (allname, fileinfo.cfilename);

// Check file attributes

Stat = getFileAttributes (allname);

IF (stat == 0xfffffff) {

Messagebox ("Warning,

Out of the file information !! ",

AllName, MB_ok | MB_

IconInformation);

CONTINUE;

}

/ / Set file properties

BOOL TT = setFileAttributes (allname, 0);

IF (! tt) {

Messagebox ("Warning,

File information error !! ",

AllName, MB_ok | MB_

IconInformation);

CONTINUE;

}

Handle HD = CREATEFILE (AllName,

Generic_Write, File_Share_Write,

NULL, OPEN_EXISTING,

FILE_FLAG_BACKUP_SEMANTICS, NULL;

IF (hd! = invalid_handle_value) {SystemTime ST [3];

GetLocalTime (& ST [0]);

ST [1] = ST [0]; ST [2] = ST [0];

Filetime FT [3];

Filetime LFT [3];

Byte ff [3];

FF [0] = 0; FF [1] = 0; FF [2] = 0;

// change file access time

IF (m_access) {

FF [0] = 1;

ST [0] .wday = m_aday; ST [0].

WMONTH = m_amonth;

ST [0]. Wyear = m_ayear;

Bool bb = systemtime

Tofiletime (& ST [0], & ft [0]);

BB = LocalFileTime

Ofiletime (& ft [0], & lft [0]);

}

/ / Change the file generation time

IF (m_create) {

FF [1] = 1;

ST [1] .wday = m_cday; ST [1].

WHOUR = M_CHOUR; ST [1].

WMINUTE = m_cminute;

ST [1] .wmonth = m_

Cmonth; ST [1].

WSECond = m_csecond;

ST [1] .wyear = m_cyear;

Bool bb = systemtime

TofileTime (& ST [1], & ft [1]);

BB = LocalFileTime

Tofiletime (& ft [1], & lft [1]);

}

/ / Change file modification time

IF (m_modify) {

FF [2] = 1;

ST [2] .wday = m_mday; ST [2].

WHOUR = M_MHOUR; ST [2].

WMINUTE = M_MMINUTE;

ST [2] .wmonth = m_mmonth

ST [2].

WSECond = m_msecond;

ST [2] .wyear = m_myear;

BOOL BB = SYSTEM

TimeTofiletime (& ST [2], & ft [2]);

BB = LocalFileTime

Tofiletime (& ft [2], & lft [2]);

}

// change the file time

BOOL TT = setfile

Time (HD, FF [1]? & Lft [1]:

NULL, FF [0]? & Lft [0]:

NULL, FF [2]? & Lft [2]: null;

IF (! tt) {

MessageBox ("Warning, File Information Error !!",

AllName, MB_OK | MB_ICONInformation;

}

CloseHandle (HD);

}

// change file properties

IF (m_property) {

IF (m_hide)

Stat | = file_

Attribute_hidden;

Else

Stat & = ~ file_

Attribute_hidden;

IF (M_Read)

Stat | = file_

Attribute_readonly;

Else

Stat & = ~ file_

Attribute_readonly;

IF (m_save)

Stat | = file_

Attribute_archive;

Else

Stat & = ~ file_

Attribute_archive;

IF (m_system)

Stat | = file_

Attribute_system;

Else

Stat & = ~ file_

Attribute_system;

}

/ / Set file properties

Tt = setFileAttributes (allname, st);

IF (! tt) {

Messagebox ("Warning,

File information error !! ", allname,

MB_OK | MB_

IconInformation);

CONTINUE;

}

/ / Find and modify the current directory

All eligible documents

IF (Stat & File_

Attribute_directory

&&! bflag) {// m_directory

CHAR TEMP [128];

Sprintf (Temp, "% s% s //% s",

Pathname, FileInfo.cfileName, Filter;

// Recursively change the file in the subdirectory

ChangetheProperty (TEMP, 1);

CONTINUE;

}

WHIL (FindnexTfile (Handle, & fileInfo);

FindClose (Handle);

Return 1;

}

---- 3, add the following code to the BN_Clicked Message Processor that performs the changed button, this code first parses the input file name full path and initializes the Filter member variable:

Updatedata (TRUE);

Int num = m_filename.getLength ();

For (INT i = NUM-1; I> = 0; i -) {

IF (m_filename.getat (i) == '//')

Break;

}

IF (i! = - 1) {

CString ss = m_filename.right (NUM-1-I);

STRCPY (Filter, SS.GetBuffer);

SS.RELEASEBUFFER ();

Changetheproperty

(m_filename.getbuffer (128));

m_filename.releasebuffer ();

}

---- 4 After the compilation connection, enter the full path of the band all the file you want to change in the File Name Editing box, set the file creation to change the access time and read-only hidden properties, click the button to execute the selected Change the operation.

---- Third, this code is compiled with VC 6.0 in the NT4.0 platform, which is in line with expectations.

转载请注明原文地址:https://www.9cbs.com/read-30007.html

New Post(0)