user7697718
user7697718

Reputation:

CopyFile succeed, MoveFile fails with the same parameters - C++

I am dealing with a very frustrating problem in C++. I need to use MoveFile function but this fails. Right now I am using CopyFile like this:

partialresult = L"D:\\CppWork\\test2\\decToBin.exe";
finala = L"D:\\CppWork\\test2\\PeFiles_\\decToBin.exe";

if (0 == CopyFile(partialresult, finala,b)) {/////
   DWORD err = GetLastError();
   std::cout << " -> Copy Fail" << std::endl;
}

Copy works ok but if I replace CopyFile with MoveFile (without changing the paths) fails with code 0x20 (MSDN doc ->ERROR_FILE_NOT_FOUND). Also if I try to replace MoveFile with CopyFile and then DeleteFile the file from the previews path, Copy works but Delete fails again with ERROR_FILE_NOT_FOUND.

bool b = false;
if (0 == CopyFile(partialresult, finala,b)) {//copy works but when try to delete the file give error
    DWORD err = GetLastError();
    std::cout << " -> Copy Fail" << std::endl;
    outFile   << " -> Copy Fail" << std::endl;
 }
  else {
    std::cout << " -> Copy Done" << std::endl;
    outFile   << " -> Copy Done" << std::endl;
    if (!DeleteFile(partialresult))
        DWORD err= GetLastError(); //error is "ERROR_FILE_NOT_FOUND"
  }

Has anyone some ideea what is happening here? Thanks.

Upvotes: 1

Views: 514

Answers (1)

user7697718
user7697718

Reputation:

I have solved the problem with CreateFile by marking them for delete after the process has finish like this:

 CreateFile(path, GENERIC_READ,FILE_SHARE_DELETE,NULL, OPEN_EXISTING, FILE_FLAG_DELETE_ON_CLOSE,NULL);

Thank you all :)

Upvotes: 1

Related Questions