Copy and deleting folders

mrfard
mrfard used Ask the Experts™
on
How can I copy a whole directory like "xcopy src, dest /s" would and Delete directorys in MFC/C++

Thanks
Matt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You could use system() function to call xcopy.
Example:
system("xcopy src, dest /s");
jkr
Top Expert 2012

Commented:
Use "SHFileOperation()", e.g.

SHFILEOPSTRUCT os;

ZeroMemory ( &os, sizeof ( SHFILEOPSTRUCT));

os.wFunc = FO_COPY;
os.pFrom = "c:\\src";
os.pTo = "C:\\target";
os.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;

SHFileOperation ( &os);

Commented:
using system() to run a commandline program will open a console window which is usually undesired behaviour in a GUI app.

A Win32 app can use SHFileOperation() to copy a directory tree.

See:
    http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/reference/functions/shfileoperation.asp
    http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/reference/structures/shfileopstruct.asp
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

jkr
Top Expert 2012

Commented:
Usually, I don't forget about the links :o)

Commented:
Or you can use FindFirstFile, CopyFile, and DeleteFile API functions to find, copy and delete files.

Commented:
Sorry jkr, the kids needed a bath when I was reading the Q.  Forgot to check for changes when I returned.  My thread safety sucks.

Commented:
>>os.wFunc = FO_COPY;
>>os.pFrom = "c:\\src";
>>os.pTo = "C:\\target";
>>os.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;

The following example creates a src directory under the target directory !
Someone knows how to overcome this problem ?
I though the the contents of src (including subdirectories) will be copied to c:\target but it creates first the topmost directory src under target !

Commented:
As far as I remember, the source and target parameters are a list of zero-terminated filenames with an *aditional* zero marking the end of the list.

In your case, the following should work:
os.pFrom = "c:\\src\0"; // 2 terminating zeros
os.pTo = "C:\\target\0"; // 2 terminating zeros

Commented:
alexo,
Adding an additional \0 didn't help.
It still copied the source directory parent into the target directory
I tried adding FOF_FILESONLY but it also didn't help.
This weird thing troubled me.
I tracked down the strange behavior ...
SOUNDS STRANGE BUT:
IF THE TARGET DIRECTORY EXISTS IT CREATES A SUBDIRECTORY:

I did the following:
os.pFrom = "c:\\temp\\ac3";
os.pTo = "C:\\ac5";

The first time I executed the program it copied all the files from c:\temp\ac3 to c:\ac5 like it should

The second time I executed the program (without deleting c:\ac5) it created the directory c:\ac5\ac3 and copied there all the files !!!

Commented:
How about
os.pFrom = "c:\\temp\\ac3\\*";

Commented:
>>How about
>>os.pFrom = "c:\\temp\\ac3\\*";
If the target directory do not exist nothing is copied !
If the target directory does exist the files are copied as they should !

Still there is no version that copies the files and creates the directory if it doesn't exist.
This needed for subdirectories, because none are created using the "c:\\temp\\ac3\\*"; version.
Subdirectories are copied using "c:\\temp\\ac3" but then the parent directory is created as well in the second execution !
Commented:
    SHFILEOPSTRUCT op = { 0 };
     op.wFunc = FO_COPY;
     op.pFrom = "C:\\Temp\\src\\*\0";
     op.pTo = "C:\\Temp\\dest\0";
     op.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;
     int rc = SHFileOperation(&op);

Author

Commented:
Thank you All, That worked perfectlly.

Thanks
Matt

Author

Commented:
Thank you.. I was stuck.


Thanks
Matt

Commented:
The following works fine.
It is a combination of FOF_SILENT and no * that caused the problem. No \0 are necessary.


    SHFILEOPSTRUCT op = { 0 };
    op.wFunc = FO_COPY;
    op.pFrom = "C:\\Temp\\src\\*";
    op.pTo = "C:\\Temp\\dest";
    op.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;
    int rc = SHFileOperation(&op);

Commented:
>> No \0 are necessary.
They are according to the docs.  It *may* work without them but it may also stop working suddenly.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial