mrfard
asked on
Copy and deleting folders
How can I copy a whole directory like "xcopy src, dest /s" would and Delete directorys in MFC/C++
Thanks
Matt
Thanks
Matt
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);
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);
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
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
Usually, I don't forget about the links :o)
Or you can use FindFirstFile, CopyFile, and DeleteFile API functions to find, copy and delete files.
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.
>>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 !
>>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 !
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
In your case, the following should work:
os.pFrom = "c:\\src\0"; // 2 terminating zeros
os.pTo = "C:\\target\0"; // 2 terminating zeros
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 !!!
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 !!!
How about
os.pFrom = "c:\\temp\\ac3\\*";
os.pFrom = "c:\\temp\\ac3\\*";
>>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 !
>>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 !
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you All, That worked perfectlly.
Thanks
Matt
Thanks
Matt
ASKER
Thank you.. I was stuck.
Thanks
Matt
Thanks
Matt
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);
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);
>> No \0 are necessary.
They are according to the docs. It *may* work without them but it may also stop working suddenly.
They are according to the docs. It *may* work without them but it may also stop working suddenly.
Example:
system("xcopy src, dest /s");