Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to copy directory in MFC?

1,171 views
Skip to first unread message

Landon

unread,
Aug 26, 2008, 7:40:02 AM8/26/08
to
I use Visual C++ MFC 4.2.

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.

AliR (VC++ MVP)

unread,
Aug 26, 2008, 10:27:51 AM8/26/08
to
SHFileOperation with function FO_COPY will copy a file or a directory.

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...

Landon

unread,
Aug 26, 2008, 8:20:04 PM8/26/08
to
Ok, thank you. So Tom, does Ali's suggestion code will show some stuff like
progress bar while we are copying file in Windows Explorer?

Thank you very much.

Landon

unread,
Aug 27, 2008, 3:36:01 AM8/27/08
to
Ali I've tried your code but it was RUNTIME ERROR.

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.


Landon

unread,
Aug 27, 2008, 3:44:03 AM8/27/08
to

Landon

unread,
Aug 27, 2008, 3:45:01 AM8/27/08
to

AliR (VC++ MVP)

unread,
Aug 27, 2008, 10:55:56 AM8/27/08
to
Hi Landon,

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...

Tom Serface

unread,
Aug 27, 2008, 2:31:14 PM8/27/08
to
Yes. You may find this article and sample code useful for figuring it out:

http://www.codeproject.com/KB/files/alexfileoperations.aspx

Tom

"Landon" <Lan...@discussions.microsoft.com> wrote in message

news:0CA3D0E5-67A9-4897...@microsoft.com...

Landon

unread,
Aug 27, 2008, 8:11:01 PM8/27/08
to
Oh thanks Ali.

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.

Sunny

unread,
Aug 27, 2008, 11:01:11 PM8/27/08
to

"AliR (VC++ MVP)" <Al...@online.nospam> 写入消息新闻:KpUsk.20381$89....@nlpi069.nbdc.sbc.com...

> SHFileOperation with function FO_COPY will copy a file or a directory.
>
> 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;//this parameter must be terminated with a double
> NULL character ("\0\0") to indicate the end of the buffer
file.pTo=To.GetBuffer();//this parameter must be terminated with a
double NULL character ("\0\0") to indicate the end of the buffer

AliR (VC++ MVP)

unread,
Aug 28, 2008, 10:41:17 AM8/28/08
to
Sorry I missed the fact that the To paramter was a double null terminated
string.

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...

0 new messages