Copy a Folder

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 173
Reputation: Lukezzz is an unknown quantity at this point 
Solved Threads: 1
Lukezzz Lukezzz is offline Offline
Junior Poster

Copy a Folder

 
0
  #1
May 21st, 2008
I am trying to Copy a Folder that consist of many files to another folder with a new Name.
I know how to do with files but if I write:
System::IO:: Directory:: I cant find any ::Copy Members here.
Is it possible to Copy a folder "C:\\Folder1" and at the same time rename that folder to "C:\\Folder2" that also will consist the files inside the folder.

For Files I do like this, Is Copy with folders very different ?
  1. System::IO::File::Copy("C:\\TextFile1.txt", "C:\\TextFile2.txt");
Last edited by Lukezzz; May 21st, 2008 at 6:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,609
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Copy a Folder

 
0
  #2
May 21st, 2008
Here is an example of how to copy all the subfolders and files to a new destination folder. Looks like you are using managed code, so I don't know how to use unmanaged win32 functions in managed programs.

  1. #include <windows.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. SHFILEOPSTRUCT sf;
  8. memset(&sf,0,sizeof(sf));
  9. sf.hwnd = 0;
  10. sf.wFunc = FO_COPY;
  11. sf.pFrom = "d:\\masm32\\*.*";
  12. sf.pTo = "d:\\masm33";
  13. sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;
  14.  
  15. int n = SHFileOperation(&sf);
  16. if( n == 0)
  17. {
  18. cout << "Success\n";
  19. }
  20. else
  21. {
  22. cout << "Failed\n";
  23. }
  24. }
Last edited by Ancient Dragon; May 21st, 2008 at 9:56 pm. Reason: deleted unnecessary lines
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 173
Reputation: Lukezzz is an unknown quantity at this point 
Solved Threads: 1
Lukezzz Lukezzz is offline Offline
Junior Poster

Re: Copy a Folder

 
0
  #3
May 21st, 2008
Thank you, I will take a look at that...
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 6
Reputation: pigg is an unknown quantity at this point 
Solved Threads: 0
pigg pigg is offline Offline
Newbie Poster

Re: Copy a Folder

 
0
  #4
Feb 16th, 2009
Thanks very much for your answer.
I have another problem now:
if i want to use the 'string' to be pFrom&pTo, what should i do?
E.g:
string ss = "d:\\masm32\\*.*;
sf.pFrom = ss;

it got errors. could you help me?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 6
Reputation: pigg is an unknown quantity at this point 
Solved Threads: 0
pigg pigg is offline Offline
Newbie Poster

Re: Copy a Folder

 
0
  #5
Feb 16th, 2009
Originally Posted by pigg View Post
Thanks very much for your answer.
I have another problem now:
if i want to use the 'string' to be pFrom&pTo, what should i do?
E.g:
string ss = "d:\\masm32\\*.*;
sf.pFrom = ss;

it got errors. could you help me?
Sorry, here is the code of what i want to do(with compile error):
  1. #include <windows.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. SHFILEOPSTRUCT sf;
  8. memset(&sf,0,sizeof(sf));
  9. sf.hwnd = 0;
  10. sf.wFunc = FO_COPY;
  11. string ss = "d:\\masm32\\*.*";
  12. string st = "d:\\masm33";
  13. sf.pFrom = ss;
  14. sf.pTo = st;
  15. sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;
  16.  
  17. int n = SHFileOperation(&sf);
  18. if( n == 0)
  19. {
  20. cout << "Success\n";
  21. }
  22. else
  23. {
  24. cout << "Failed\n";
  25. }
  26. }
Last edited by Ancient Dragon; Feb 16th, 2009 at 10:24 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,966
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: Copy a Folder

 
0
  #6
Feb 16th, 2009
You can't convert a std::string to a LPCSTR, you'll have to convert it manually. This can be don with the std::string memeber c_str() So change this:
  1. sf.pFrom = ss;
  2. sf.pTo = st;
to this:

  1. sf.pFrom = ss.c_str();
  2. sf.pTo = st.c_str();

And it should work!

Next time if you post code use code-tags.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 6
Reputation: pigg is an unknown quantity at this point 
Solved Threads: 0
pigg pigg is offline Offline
Newbie Poster

Re: Copy a Folder

 
0
  #7
Feb 16th, 2009
Thank you so much!

i tried, and it does not work.
i have added the ".c_str()", and the program pass the compile without errors. however, the copy was failed...

could you give me more suggestions?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,966
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: Copy a Folder

 
0
  #8
Feb 16th, 2009
Does it print 'succes' or 'failed'?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 6
Reputation: pigg is an unknown quantity at this point 
Solved Threads: 0
pigg pigg is offline Offline
Newbie Poster

Re: Copy a Folder

 
0
  #9
Feb 16th, 2009
Originally Posted by niek_e View Post
Does it print 'succes' or 'failed'?
sorry, "failed" was printed.
and here is my code, i can not understand why...

  1. #include "stdafx.h"
  2. #include <windows.h>
  3. #include <iostream>
  4. #include <CString>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. SHFILEOPSTRUCT sf;
  10. memset(&sf,0,sizeof(sf));
  11. sf.hwnd = 0;
  12. sf.wFunc = FO_COPY;
  13.  
  14. string from = "D:\\testfrom\\*.*";
  15. string to = "D:\\testto";
  16.  
  17. sf.pFrom = from.c_str();
  18. sf.pTo = to.c_str();
  19. sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;
  20.  
  21. int n = SHFileOperation(&sf);
  22. if( n == 0)
  23. {
  24. cout << "Success\n";
  25. }
  26. else
  27. {
  28. cout << "Failed\n";
  29. }
  30. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,966
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

Re: Copy a Folder

 
0
  #10
Feb 17th, 2009
Here's the MSDN page for SHFileOperation. At the bottom of the page are all the error-codes (return values). So what value does it return in your case?
Print out the value of 'n' in the else statement to find out and thus find out what went wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC