Movefile, movefileex, copyfile experiences C ++

Source: Internet
Author: User

Keywords:

Movefile, movefileex, copyfile, file read-only attribute, file hiding attribute,

Getfileattributes, setfileattributes, read and set file attributes

1. Effect of read-only and hidden attributes of files on Functions

1.1 mobile operations (movefile and movefileex)

Testcase 1: Set the source file to C:/file1, the target file file2, and the target file does not exist.

Test results: the read-only and hidden attributes of file1 have no effect on the above two functions. After the function returns, file1 is renamed to file2. File attributes remain unchanged

Testcase 2: the target file file2 exists and its attribute is read-only.

Test result: movefile fails and is irrelevant to the read-only attribute of file2 because it does not support overwriting existing files.

When the movefileex call flag is set to movefile_replace_existing, the read-only attribute of file2 determines whether the call is successful, and is irrelevant to the hidden attribute.

1.2 copy operation (copyfile)

Msdn has the following declaration in both function documents:

This function fails with error_access_denied if the destination file already exists and has the file_attribute_hidden or file_attribute_readonly attribute set.

You only need to pay attention to it.

2. Cross-volume operations

1.1 mobile operations

Movefile supports cross-volume operations. See the description in msdn:

A new file may be on a different file system or drive. A new directory must be on the same drive

Movefileex also supports cross-volume operations, but you need to set the movefile_copy_allowed flag

1.2 copy operation

Supports cross-volume operations

3. Set file attributes. The following uses the read-only attribute as an example.

Use the setfileattributes and getfileattributes functions for file attribute operations

For more information, see msdn.

Operate instances

// Test whether a property is set. Take the read-only property as an example.

// Obtain existing file attributes

Cstring strpathname = _ T ("path and name of some file ");

Tchar szerr [255];

DWORD dwattrs =: getfileattributes (strpathname );

If (dwattrs! = Invalid_file_attributes)

{

If (dwattrs & file_attribute_readonly)

{

// Read-only attribute

// Cancel the read-only attribute

Dwattrs & = ~ File_attribute_readonly;

}

Else

{

// No read-only attribute

// Set the read-only attribute

Dwattrs | = file_attribute_readonly;

// Set attributes to a file

If (! : Setfileattributes (strpathname, dwattrs ))

{

// Print the error message

: Formatmessage (format_message_from_system, null,: getlasterror (), 0, szerr, 255, null );

}

}

}

Else

{

: Formatmessage (format_message_from_system, null,: getlasterror (), 0, szerr, 255, null );

}

Operation Result:

If the file has a read-only attribute, the attribute is canceled. Otherwise, the attribute is set.

Note:

Before setting file properties, you must first obtain the current file properties, and then set or cancel a property on the basis of the current property to avoid other attributes being unintentionally canceled.

4. Summary

When using movefileex, consider the read-only attribute of the target file. When using copyfile, consider the read-only and hidden attributes of the target file.

Movefile does not support overwriting the target file.

Movefile supports cross-volume operations. You can move the file to another logical partition or physical media. movefileex also supports the same operation, but sets the movefile_copy_allowed flag.

Copyfile supports cross-volume operations

Copyfileex is not involved in this article. The code that calls it cannot be compiled successfully in my environment.

When File Operations contain the target file, the read-only and hidden attributes of the file must be taken into account. when you are sure to copy and move the object to the target file, you can first remove its read-only and hidden attributes. After the operation is complete, you can decide whether to restore the attributes back as needed. check whether the target file is on another volume.

Movefileex functions are powerful, but it only supports Windows XP and later operating systems. Therefore, movefileex needs to be used with caution.

Copyfileex, getfileattributesex, and setfileattributesex support Windows 98 and later operating systems.

File operations are very common. Pay attention to these details. Otherwise, the program may crash due to a failed operation on the mobile file, or the program cannot run on the host system because of the use of advanced functions.

Keywords:

Movefile, movefileex, copyfile, file read-only attribute, file hiding attribute,

Getfileattributes, setfileattributes, read and set file attributes

1. Effect of read-only and hidden attributes of files on Functions

1.1 mobile operations (movefile and movefileex)

Testcase 1: Set the source file to C:/file1, the target file file2, and the target file does not exist.

Test results: the read-only and hidden attributes of file1 have no effect on the above two functions. After the function returns, file1 is renamed to file2. File attributes remain unchanged

Testcase 2: the target file file2 exists and its attribute is read-only.

Test result: movefile fails and is irrelevant to the read-only attribute of file2 because it does not support overwriting existing files.

When the movefileex call flag is set to movefile_replace_existing, the read-only attribute of file2 determines whether the call is successful, and is irrelevant to the hidden attribute.

1.2 copy operation (copyfile)

Msdn has the following declaration in both function documents:

This function fails with error_access_denied if the destination file already exists and has the file_attribute_hidden or file_attribute_readonly attribute set.

You only need to pay attention to it.

2. Cross-volume operations

1.1 mobile operations

Movefile supports cross-volume operations. See the description in msdn:

A new file may be on a different file system or drive. A new directory must be on the same drive

Movefileex also supports cross-volume operations, but you need to set the movefile_copy_allowed flag

1.2 copy operation

Supports cross-volume operations

3. Set file attributes. The following uses the read-only attribute as an example.

Use the setfileattributes and getfileattributes functions for file attribute operations

For more information, see msdn.

Operate instances

// Test whether a property is set. Take the read-only property as an example.

// Obtain existing file attributes

Cstring strpathname = _ T ("path and name of some file ");

Tchar szerr [255];

DWORD dwattrs =: getfileattributes (strpathname );

If (dwattrs! = Invalid_file_attributes)

{

If (dwattrs & file_attribute_readonly)

{

// Read-only attribute

// Cancel the read-only attribute

Dwattrs & = ~ File_attribute_readonly;

}

Else

{

// No read-only attribute

// Set the read-only attribute

Dwattrs | = file_attribute_readonly;

// Set attributes to a file

If (! : Setfileattributes (strpathname, dwattrs ))

{

// Print the error message

: Formatmessage (format_message_from_system, null,: getlasterror (), 0, szerr, 255, null );

}

}

}

Else

{

: Formatmessage (format_message_from_system, null,: getlasterror (), 0, szerr, 255, null );

}

Operation Result:

If the file has a read-only attribute, the attribute is canceled. Otherwise, the attribute is set.

Note:

Before setting file properties, you must first obtain the current file properties, and then set or cancel a property on the basis of the current property to avoid other attributes being unintentionally canceled.

4. Summary

When using movefileex, consider the read-only attribute of the target file. When using copyfile, consider the read-only and hidden attributes of the target file.

Movefile does not support overwriting the target file.

Movefile supports cross-volume operations. You can move the file to another logical partition or physical media. movefileex also supports the same operation, but sets the movefile_copy_allowed flag.

Copyfile supports cross-volume operations

Copyfileex is not involved in this article. The code that calls it cannot be compiled successfully in my environment.

When File Operations contain the target file, the read-only and hidden attributes of the file must be taken into account. when you are sure to copy and move the object to the target file, you can first remove its read-only and hidden attributes. After the operation is complete, you can decide whether to restore the attributes back as needed. check whether the target file is on another volume.

Movefileex functions are powerful, but it only supports Windows XP and later operating systems. Therefore, movefileex needs to be used with caution.

Copyfileex, getfileattributesex, and setfileattributesex support Windows 98 and later operating systems.

File operations are very common. Pay attention to these details. Otherwise, the program may crash due to a failed operation on the mobile file, or the program cannot run on the host system because of the use of advanced functions.

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.