### Copyfile

ok, so first of all I'm a complete noob in programming and I've been trying to teach myself on and off for a little now, so don't be too hard on me ;)

I was trying to write a program that will copy itself into another folder (seems like an easy enough program), but i cant seem to figure it out.

#include <iostream>
#include <windows.h>

using namespace std;

string getlocation;

int main ()
{
cin>>getlocation;
if (CopyFile (getlocation, "C:\\end destination"))
cout << "File could not be copied successfully";

else
cout << "File copied successfully!";

cin.get (); // pause for input
return EXIT_SUCCESS; // program was executed successfully
}

the reason i used getlocation is to find the path to the file in the first place, in case the file were to be on a different computer and I didn't have the file path.
this is the error: cannot convert std::string' to const CHAR*' for argument 1' to BOOL CopyFileA(const CHAR*, const CHAR*, BOOL)'
You can't use an std::string there, it wants a const char *. Look up the .c_str() function of std::string.
i looked at that, and googled it as well, and my new code is as follows:

#include <iostream>
#include <windows.h>
#include <conio.h>

using namespace std;

int main (int argc, char *argv[])
{
if (CopyFile (argv[0], "C:\\test.exe", true))
cout << "File could not be copied successfully";

else
cout << "File copied successfully!";

cin.get (); // pause for input
return EXIT_SUCCESS; // program was executed successfully
}

it compiles fine, but it doesn't copy the file....; where did I mess up this time?
CopyFile() returns non-zero if you succeed, not if you fail. Your if/else printing statements are switched.
easy mistake there. Apparently I messed up with the path, I jsut changed the path and it worked fine. Thanks for the help!
alright, now that i've been messing around with this function a little I have another question - It seems I can copy to some locations, but not to all. I run the computer as an administrator, so I'm not sure why it does not copy itself into certain paths. Any thoughts?
Can you post examples of where it doesn't work. Do you know what the 3rd parameter does? You might want to check that you're using it correctly.
http://msdn.microsoft.com/en-us/library/aa363851(VS.85).aspx
im pretty sure im using the 3rd parameter correctly - this way it doesn't overwrite the file if it would already exists. examples of where it doesn't work is in C:\\ as a new file there, or in the startup folder for example - I will test some more in a little
yeah, i just remember one program that a friend of mine wrote (he was using xp at the time) and it didn't matter where he tried to copy it to, it would do it. Did vista perhaps add more security?
The restriction I mentioned above applies to XP, as well.
so i just tested running the .exe file as an administrator and it works that way. Is there a way to not have to run the program as an administrator and still achieve that result?
and yet another question to further this program a little more;
i've edited my program a bit and now I want it to generate a name that will always be one larger than the previous, aka the first one named test1.exe the second test2.exe etc.
i thought I coult achieve this with the following code, but i get an error:expected )' before "i"

#define _WIN32_WINNT 0x0500
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

string Todd;

int main(int argc, char *argv[]){
for(int i=1; i<3; ++i)
{
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd,SW_HIDE);
CopyFile (argv[0], "C:\\Users\\test"i".exe", false);
}
return 0;
}

any ideas to help a noob out again? :)

edit: i looked at how to get a random number, but I'm uncertain as to how to insert that number into the name of the file:

int random_integer = rand();
CopyFile (argv[0], "C:\\Users\\test"random_integer".exe, false);

Last edited on
Vista does have further permissions than XP. XP was positively unsecured. All of these security facilities have been available since NT 3.5 (3.0 wasn't really used), but largely ingored until recent releases.

"C:\\Users\\test"i".exe" isn't a string. You need to format it using an ostringstream or snprintf.
 123 std::ostringstream os; os << "C:\\Users\\test" << i << ".exe"; std::string filename - os.str();

A application can run at higher privilege than its user. You have to do stuff though.
so i just edited it again, but unfortunately I'm still getting an error.
cannot convert std::string' to const CHAR*' for argument 2' to BOOL CopyFileA(const CHAR*, const CHAR*, BOOL)'
i looked at ostringstream and .str() functions, but couldn't find anything that could resolve that...

using namespace std;

string filename;
ostringstream os;

int main(int argc, char *argv[]){

for(int i=1; i<5; ++i)
{
os << "C:\\Users\\test" <<i<<".exe";
filename = os.str();
CopyFile (argv[0], filename, false);
}
return 0;
}
CopyFile (argv[0], filename.c_str(), false);`
sweet, a huge thanks to everyone who helped. Ya'll are awesome.
Topic archived. No new replies allowed.