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");

Recommended Answers

All 14 Replies

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

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

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?

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

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.

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?

Does it print 'succes' or 'failed'?

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

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.

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?

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.

commented: Good catch, thanks! +14

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;

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());
commented: Nearly a year late, your post trashes memory AND you forgot to read post #12 - fails all round -4

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.