I need to explore a directory and copy all DIRECTORIES inside it and their
contents.
How to copy directory in MFC? I only know about CopyFile function?
Thank you very much.
Here is an example:
void CopyDirectory(CString From,CString To)
{
SHFILEOPSTRUCT file;
char FromFile[255];
//add an extra NULL to the end of From
strcpy(FromFile,From);
FromFile[strlen(FromFile)+1]=NULL;
file.hwnd=::GetActiveWindow();
file.wFunc=FO_COPY;
file.pFrom=FromFile;
file.pTo=To.GetBuffer();
file.fFlags=FOF_NOCONFIRMATION | FOF_SIMPLEPROGRESS;
file.fAnyOperationsAborted=0;
file.hNameMappings=NULL;
file.lpszProgressTitle=_T("Copy My Directory");
SHFileOperation(&file);
To.ReleaseBuffer();
}
AliR.
"Landon" <Lan...@discussions.microsoft.com> wrote in message
news:121DA84C-CC53-46B9...@microsoft.com...
Thank you very much.
The error occured when it reached the SHFileOperation Line.
The RUNTIME ERROR was:
"Cannot copy a file. Cannot read from a file of the forwarding side or a
disk."
and also the RETURN VALUE of the SHFileOperation was 1026 when I traced it.
It should have been 0 if success, right?
What caused this error and how to solve this?
Thank you very much.
What is the path that you are sending to the CopyDirectory function I
posted?
Error 1026 (0x402) is described in the docs as : An unknown error occurred.
This is typically due to an invalid path in the source or destination. This
error does not occur on Windows Vista and later.
The code I sent you will display a progress dialog if the directory is large
enough. If you want a progress bar that displays the name of the files as
it is copying them then remove the FOF_SIMPLEPROGRESS. If you want other
warnings then simply remove the FOF_NOCONFIRMATION flag also.
AliR.
"Landon" <Lan...@discussions.microsoft.com> wrote in message
news:0CEAA6FB-E33A-40AE...@microsoft.com...
http://www.codeproject.com/KB/files/alexfileoperations.aspx
Tom
"Landon" <Lan...@discussions.microsoft.com> wrote in message
news:0CA3D0E5-67A9-4897...@microsoft.com...
I've fixed the error yesterday, here is the new code:
SHFILEOPSTRUCT file;
char* pSrc = new char[Src.GetLength() + 2];
memset( pSrc, NULL, Src.GetLength() + 2 );
memcpy( pSrc, Src.GetBuffer( Src.GetLength() ), Src.GetLength() );
char* pDest = new char[Dest.GetLength() + 2];
memset( pDest, NULL, Dest.GetLength() + 2 );
memcpy( pDest, Dest.GetBuffer( Dest.GetLength() ), Dest.GetLength() );
file.hwnd = ::GetActiveWindow();
file.wFunc = FO_COPY;
file.pFrom = pSrc;
file.pTo = pDest;
file.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_SILENT;
file.fAnyOperationsAborted = 0;
file.hNameMappings = NULL;
//file.lpszProgressTitle = _T( "Copy Directory" );
int i = SHFileOperation( &file );
Src.ReleaseBuffer();
Dest.ReleaseBuffer();
delete[] pSrc;
delete[] pDest;
I thought the error came from missing of NULL addition at the end of the
string.
Like the SHFILEOPSTRUCT structure said, that we must add another NULL at the
end of the string.
Thank you.
I would use strcpy instead of memcpy, but either way.
AliR.
"Landon" <Lan...@discussions.microsoft.com> wrote in message
news:FEA45402-979C-47F8...@microsoft.com...