Search code examples
c++windowswinapifile-copying

CopyFile fails with error code 3 (ERROR_PATH_NOT_FOUND)


So, before starting, I have been researching (not only on Stack Overflow) and I can't find a solution to my problem.

I am trying to copy a file to a certain place (and if possible, change its name at the same time I copy it) using Windows' CopyFile function.

I've created a sample program to show the error.

#include <iostream>
#include <Windows.h>
using namespace std;

int main()
{
    cout << "Copy in progress" << endl;
    bool X = CopyFile(L"test.txt", L"C:\\", 1); //NOT C: nor C:\\ 
    if (!X){ cout << "FALLO AL COPIAR\n"; cout << "Error: "<<GetLastError();}
    else{ cout << "COPIADO CORRECTO";  }
    cin.get(); cin.get();
    return 0;
}

GetLastError() returns 3 - which means ERROR_PATH_NOT_FOUND - but, believe me, I've checked every file (test.txt is in the same place as the built .exe, I'm running as admin...) and it still gives 3.

I can't manage to make it work. Notice the L"test" this is done because I use Visual Studio with certain character set settings, I've tried to change that config and use "test.txt" -> Still error 3.


Solution

  • You need to provide a filename:

    bool X = CopyFile(L"test.txt", L"C:\\test.txt", 1);