Search code examples
c++winapimovefileex

MoveFile() Works, but returns error 2 (file not found)


I'm using the function Movefile() (in C). I can see the file moved from the source folder to the destination (means the MoveFile success) but when I do GetLastError() I get error no. 2 (ERROR_FILE_NOT_FOUND).

What can be the problem?

The code is:

_snprintf(szSrcPath, MAX_PATH, "%s/%s/%s.jpg", NPath, imagePathFromAdmin, username);
_snprintf(szDestPath, MAX_PATH, "%s/Images/Storage/%s/%d/%s.jpg", NPath, domain, sub_folder, username);
strcpy(imagePathStorgae,szDestPath);
MoveFile(szSrcPath,szDestPath);
err=GetLastError();

Solution

  • Do not get the error code if a function succeeds, the value is not valid. Instead check the value returned by the actual function (i.e. the MoveFile function return value) and if that indicates that an error happened, then you can check what the error was.