CopyFile error 5, access denied

Alrighty so I was messing around building some stuff in c++ because that is what us nerdy computer science majors do inbetween semesters right? So I am trying to copy files, but the windows + compiler gods will not submit easily today. In my code below I get the sysdirectory + the file I want to copy with:
GetSystemDirectory(sysdirect, sizeof(sysdirect)); //Sets system directory IE. C:\Windows\System32
	GetModuleFileName(GetModuleHandle(NULL), cfilename, sizeof(cfilename));
	_splitpath(cfilename, NULL, NULL, fileName, extension); //splits the path name up into different pieces
	_snprintf(rfilename, sizeof(rfilename), "%s%s", fileName, extension); // writes formatted data to a string 

if (strstr(cfilename, sysdirect) == NULL) { //returns a pointer to the first occurence of str2 in str1
		char tmpfilename[MAX_PATH]; sprintf(tmpfilename, "%s\\%s", sysdirect, filename);
		cout << tmpfilename << endl;
		bool debug = CopyFile(cfilename, tmpfilename, FALSE);
		if (!debug) {
		cout << "Error: " << GetLastError() << endl;
		} else {
		cout << "Okay " << endl;

my tmpfilename prints exactly where I want it to go and my debug prints out an error 5 which is an access denied error. I was looking at the system("copy") function, but I don't think I would be able to use my pathnames. From what it looks like I would have to hardcode in like "copy C:\windows\system32". Is there any way around this? -Cheers and happy holidays.
CopyFile() doesn't work on directories. When copying folders and their contents you would use CreateDirectory() and pass it the name of the original folder, you should also check it's other attributes and set those to be the same in the folder you create as well, but that can probably wait until later.

To accomplish something like what you are trying you first write a function that enumerates all of the files in a given directory, with FindFirstFile() and FindNextFile(), and keeps calls CreateDirectory() on each item whose dwFileAttributes data member comes back with the 'FILE_ATTRIBUTE_DIRECTORY' flag marked and CopyFile() on which ever ones do not. You then recursively call this function for every folder it encounters.

- CreateDirectory():

- FindFirstFile():

- FindNextFile():


And for when you are done.

- FindClose():
Topic archived. No new replies allowed.