954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Copy a Folder

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 ?

System::IO::File::Copy("C:\\TextFile1.txt", "C:\\TextFile2.txt");
Lukezzz
Posting Whiz in Training
268 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

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.

#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";
    }
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thank you, I will take a look at that...

Lukezzz
Posting Whiz in Training
268 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

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?

pigg
Newbie Poster
6 posts since Feb 2009
Reputation Points: 10
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?

Sorry, here is the code of what i want to do(with compile error):

#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";
    }
}
pigg
Newbie Poster
6 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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:

sf.pFrom = ss;
    sf.pTo = st;

to this:

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


And it should work!

Next time if you post code use code-tags .

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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?

pigg
Newbie Poster
6 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Does it print 'succes' or 'failed'?

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
Does it print 'succes' or 'failed'?


sorry, "failed" was printed.
and here is my code, i can not understand why...

#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";
    }
}
pigg
Newbie Poster
6 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
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.

Thanks so much for your reply.
Yes, as your advice, i add the output of "n",
the result is "1026".(it should be 0x402)

By the MSDN:
0x402 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.

So i guess the fail may from the "invalid path". is it right?
(But in my PC, the path is true, with out any problem.)
Could you help me and give me some suggestions?

pigg
Newbie Poster
6 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Yes, as your advice, i add the output of "n", the result is "1026".(it should be 0x402)

By the MSDN: 0x402 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.

So i guess the fail may from the "invalid path". is it right? (But in my PC, the path is true, with out any problem.)

Both the pFrom and pTo members need to be double-null terminated, see the SHFILEOPSTRUCT Structure documentation.

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

Thank you so much for your advice.

The problem is solved.
I have add a "\0" to the end of the string, which should be a double null terminated(i think).
anyway, here is the solution:

std::string testto = "d:\\testto"
    SHFILEOPSTRUCT sf;
    memset(&sf,0,sizeof(sf));
	sf.hwnd = 0;
    sf.wFunc = FO_COPY;
	sf.pFrom = "d:\\testfrom";
	char d_te[] = "\0";
	std::string to = testto;
	to.append(d_te);
	sf.pTo = (LPCSTR) to.c_str();
    sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_SILENT;
pigg
Newbie Poster
6 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

sorry, "failed" was printed. and here is my code, i can not understand why...

#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";
    }
}



Try this...

string from = "D:\\testfrom\\*.*";
	string to = "D:\\testto";
    
	sf.pFrom = (char*)malloc(from.length());
	sf.pTo = (char*)malloc(to.length());
            strcpy(sf.pFrom,(char*)from.c_str());
            strcpy(sf.pTo,(char*)to.c_str());
wavsyntax
Newbie Poster
8 posts since May 2009
Reputation Points: 6
Solved Threads: 0
 

Hello,

I would really appreciate some help:
I’m using SHFileOperation function for copying folders from one place to another and it works great on XP OS.
Unfortunately, this function doesn’t work on Vista OS.
I’m wondering if somebody knows which function will work on both of them.

Thanks in advance,
Julia

JuliaSh
Newbie Poster
1 post since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You