The essence of the problem:

When copying a file to a directory where a file with the same name already exists with the ReadOnly attribute, the CopyFile copy function returns FALSE. One solution is to remove the RO attribute from the target file and retry the copy, which significantly slows down the process when copying large volumes of small files (100k +). Are there any methods that allow copying a ro-file into another ro-file into 1 action?

Code:

CString sFrom = _T("D:\test1\test.log"); CString sTo = _T("D:\test2\test.log"); // Этот файл должен быть read-only BOOL bRet = CopyFile(sFrom, sTo, FALSE); if ( FALSE == bRet ) { DWORD dwAttr = GetFileAttributes(sTo); dwAttr &= ~FILE_ATTRIBUTE_READONLY; SetFileAttributes(sTo, dwAttr); bRet = CopyFile(sFrom, sTo, FALSE); } return bRet; 
  • one
    It would be good to have GetLastError() check on error - PinkTux
  • one
    system("xcopy /r /y src dst"); - αλεχολυτ
  • @alexolut, ATP, while there is no way to test on a large volume of files, but I think it will come down. - goldstar_labs

1 answer 1

You cannot do this by one action. Attributes are designed to protect against erroneous or malicious actions, so all operations with them must be performed explicitly. If the file is set to read-only, then apparently this makes sense. Think maybe you are doing something wrong.