I am using Windows 7 Professional and I am using SHFileOperation()
to recursive copy one folder contents to another. But there is a locked file (opened exclusively by an application); I need to skip it, but SHFileOperation()
returns 0x20 when tries to copy this file.
How can I skip this file during the file copy operation?
UPDATE: this is the code:
//
// CopyDirectory()
// рекурсивное копирование содержимого одной директории в другую средствами Windows
// lpszSource - исходная папка
// lpszDestination - папка назначения
//
BOOL CopyDirectory( LPSTR lpszSource, LPSTR lpszDestination )
{
LPSTR lpszNewSource = NULL;
// структура операции с файлами
SHFILEOPSTRUCT fileOP = { 0 };
// выделим память под новый путь
lpszNewSource = (LPSTR)calloc(strlen(lpszSource) + 50, 1);
// запишем новый путь с маской
wsprintf(lpszNewSource, "%s\\*", lpszSource);
// запишем параметры операции копирования
fileOP.wFunc = FO_COPY;
fileOP.pTo = lpszDestination;
fileOP.pFrom = lpszSource;
fileOP.fFlags = FOF_SILENT | FOF_NOCONFIRMMKDIR | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NO_UI;
// выполняем операцию
INT retVal = SHFileOperation( &fileOP );
// освободим память
FREE_NULL(lpszNewSource);
DebugPrint(DEBUG_INFO, "retVal = %d\n", retVal);
// возвращаем результат копирования
return retVal == 0;
}
The
SHFileOperation()
documentation says:In your case,
0x20
is not one of the pre-Win32 error codes, so it maps to a standard Win32 error code, specificallyERROR_SHARING_VIOLATION
, which is appropriate for your situation as one of the files could not be accessed.To skip the offending file, enable the
FOF_NOERRORUI
flag in theSHFILEOPSTRUCT::fFlags
field. TheSHFILEOPSTRUCT
documentation says only the following about that flag:However, the documentation does also say this:
The documentation for
IFileOperation
(which replacesSHFileOperation()
on Vista and later) has more to say about theFOF_NOERRORUI
flag:So, with
FOF_NOERRORUI
enabled, the return value ofERROR_SHARING_VIOLATION
, and also theSHFILEOPSTRUCT::fAnyOperationsAborted
field being set to TRUE, will tell you that a file could not be accessed during the copy, but not which file specifically. It does not mean that the entireSHFileOperation()
task failed completely.