| | |
Copy a Folder
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 173
Reputation:
Solved Threads: 1
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 ?
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 ?
C++ Syntax (Toggle Plain Text)
System::IO::File::Copy("C:\\TextFile1.txt", "C:\\TextFile2.txt");
Last edited by Lukezzz; May 21st, 2008 at 6:13 pm.
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.
C++ Syntax (Toggle Plain Text)
#include <windows.h> #include <iostream> using namespace std; int main() { SHFILEOPSTRUCT sf; memset(&sf,0,sizeof(sf)); sf.hwnd = 0; sf.wFunc = FO_COPY; sf.pFrom = "d:\\masm32\\*.*"; sf.pTo = "d:\\masm33"; sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI; int n = SHFileOperation(&sf); if( n == 0) { cout << "Success\n"; } else { cout << "Failed\n"; } }
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.
•
•
Join Date: Feb 2009
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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?
C++ Syntax (Toggle Plain Text)
#include <windows.h> #include <iostream> using namespace std; int main() { SHFILEOPSTRUCT sf; memset(&sf,0,sizeof(sf)); sf.hwnd = 0; sf.wFunc = FO_COPY; string ss = "d:\\masm32\\*.*"; string st = "d:\\masm33"; sf.pFrom = ss; sf.pTo = st; sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI; int n = SHFileOperation(&sf); if( n == 0) { cout << "Success\n"; } else { cout << "Failed\n"; } }
Last edited by Ancient Dragon; Feb 16th, 2009 at 10:24 am. Reason: add code tags
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:
to this:
And it should work!
Next time if you post code use code-tags.
C++ Syntax (Toggle Plain Text)
sf.pFrom = ss; sf.pTo = st;
C++ Syntax (Toggle Plain Text)
sf.pFrom = ss.c_str(); sf.pTo = st.c_str();
And it should work!
Next time if you post code use code-tags.
•
•
Join Date: Feb 2009
Posts: 6
Reputation:
Solved Threads: 0
sorry, "failed" was printed.
and here is my code, i can not understand why...
and here is my code, i can not understand why...
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include <windows.h> #include <iostream> #include <CString> using namespace std; int main() { SHFILEOPSTRUCT sf; memset(&sf,0,sizeof(sf)); sf.hwnd = 0; sf.wFunc = FO_COPY; string from = "D:\\testfrom\\*.*"; string to = "D:\\testto"; sf.pFrom = from.c_str(); sf.pTo = to.c_str(); sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI; int n = SHFileOperation(&sf); if( n == 0) { cout << "Success\n"; } else { cout << "Failed\n"; } }
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.
Print out the value of 'n' in the else statement to find out and thus find out what went wrong.
![]() |
Similar Threads
- Lost folder in Outlook 2003 (Windows Software)
- Copy Folder with %Computername% and %Username% prefix (Visual Basic 4 / 5 / 6)
- Folder Settings (Windows NT / 2000 / XP)
- Data Error (CRC) with a folder (Windows NT / 2000 / XP)
- Can I move ALL my Applications folder from one hard drive to another? (IT Professionals' Lounge)
- duplication of picture when trying to copy paste (Windows NT / 2000 / XP)
- Ditto (OS X)
- Copy Files and Folders to CDs (Windows tips 'n' tweaks)
- xp Picture folder (Windows NT / 2000 / XP)
- Saving Address book and Messages in Outlook Express (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: What is wrong?..!
- Next Thread: How to use an array element in if-statement?
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






