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:
1
2
3
4
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 


Then
1
2
3
4
5
6
7
8
9
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.
bump
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(): http://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx

- FindFirstFile(): http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx

- FindNextFile(): http://msdn.microsoft.com/en-us/library/windows/desktop/aa364428(v=vs.85).aspx

- WIN32_FIND_DATA: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365740(v=vs.85).aspx

And for when you are done.

- FindClose(): http://msdn.microsoft.com/en-us/library/windows/desktop/aa364413(v=vs.85).aspx
Topic archived. No new replies allowed.