1

I'm trying to move some files from a directory into another directory on the same drive. All destination directories exist but MoveFile() doesn't work and GetLastError() returns 0. I'm new to C++ and i have no idea what is causing this problem. here's the code:

try
{
  destinationFilePath = i->FilePath;
  destinationFilePath=destinationFilePath.substr(destinationFilePath.find(GENERAL_SAVE_PATH) + strlen(GENERAL_SAVE_PATH.c_str()));
  destinationFilePath = Backup_Save_Path + destinationFilePath;

  vector<string> folders;
  StringSplit(destinationFilePath,"\\",folders);
  string pathToCreate;
  string backSlash = "\\";
  for(vector<string>::const_iterator j = folders.begin(); j != folders.end(); ++j)
  {
      pathToCreate += j->c_str() + backSlash;
      if (pathToCreate.find(".txt") == std::string::npos)
         CreateBackupFolders(pathToCreate);
  }

  if (MoveFile(lastExportedFilePath.c_str(),destinationFilePath.c_str()))
     AfxMessageBox("moved");
  else
  {
     DWORD dw = GetLastError();
     AfxMessageBox("Couldn't move");
  }

  lastExportedFilePath = i->FilePath;
}
catch(...)
{
  Log(LOG_PATH_LOG_INSERTING, LOG_TYPE_EXCEPTION, "ExportLogsToDB()", "Move", destinationFilePath, "");
}
Deadlock
  • 4,211
  • 1
  • 20
  • 25
Saleh Rezaei
  • 160
  • 4
  • 21
  • 5
    Can you please try to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us? – Some programmer dude Aug 30 '15 at 07:49
  • 1
    have you stepped with your debugger to see that the end resultings strings are correct? – David Haim Aug 30 '15 at 08:18
  • yes, both source path and destination path are correct – Saleh Rezaei Aug 30 '15 at 08:26
  • if source path and destination path are correct, can you simplify your example to string literals containing the source and destination path? For example `if (MoveFile("c:\\source.txt","c:\\dest.txt"))` – wimh Aug 30 '15 at 08:48
  • @SalehRezaei Could well be you need a wide character string to specify the Paths. – πάντα ῥεῖ Aug 30 '15 at 08:54
  • `if (MoveFile("D:\\WIM\\Logs_Alpr_Camera_Line_1\\2015_8_10\\2015_8_10_16.txt","D:\\LogBackup\\Logs_Alpr_Camera_Line_1\\2015_8_10\\2015_8_10_16.txt"))` – Saleh Rezaei Aug 30 '15 at 09:18
  • well I can assure you that `MoveFile` works. this API exists from the 80's. a different title such as "Failing to use MoveFile" is more appropriate. I suggest you use `GetLastError` to see what is the error – David Haim Aug 30 '15 at 09:46
  • @DavidHaim cars work since i don't know when but when your car doesn't work appropriately, you say "my car doesn't work" not "failing to use my car". if you've read the question, i said that `GetLastError` returns 0 – Saleh Rezaei Aug 30 '15 at 09:58
  • You are asking us because we are the experts, and you are a self-confessed beginner. How about you stop arguing with us and do as we ask. Provide an [MCVE]. – David Heffernan Aug 30 '15 at 12:55
  • Put `TRACE("%d\n", dw);` after `GetLastError()`. It won't say zero if `MoveFile` failed. – Barmak Shemirani Aug 30 '15 at 15:36

1 Answers1

0

Any reasons on using MoveFile over CopyFile and also not using MoveFileEx. According to Microsoft's MSDN Documentation and it verbatimly states that when you use MoveFile it does not copy the security description with it and you will lose to whatever is default over at the new location, that was on a side note.

But since its returning 0 meaning it failed we can extend that error by calling GetLastError API and looking up the resulting number to the appropriate error string so it is more meaningful. Other than randomly shooting in the dark like this. Also, is this project using Unicode/Ansi or Not-Set for the default character set?

Also, posting the whole problem without giving us a regression test so we can evaluate it and tell you what went wrong is not very helpful. You are following a line where you want someone to quickly fix your problem and get it over with, whilst this is a learning place for you and for others as well.

halsten
  • 122
  • 11
  • thanks for your answer, but i don't want to just get the answer quickly and fix the problem, if so i would have post this question 2 days ago. as i said i'm new to C++ and i have done what ever i could imagine to fix the problem, but i couldn't so i asked it here. I really want to know what's causing this problem – Saleh Rezaei Aug 30 '15 at 10:09
  • In the paragraph before the last I suggested you use `GetLastError` to extend the error base in to something more meaningful, but you failed to supply that. Instead you have have been whining and just devoting people when they are actually trying to help you. You should use a regress tests case and a simplified one so we can also try it out and also commend on our experienced vs your experience. But, you are doing so. So please be part of the solution and don't just whine randomly devote people trying to help you! – halsten Aug 30 '15 at 13:23
  • @halsten, he can't "devote" you because he doesn't have enough points. I think you need 100 points to down vote answers. – Barmak Shemirani Aug 30 '15 at 15:07
  • @BarmakShemirani Ok, makes sense. I am still new so I am picking up all the rules slowly. :) – halsten Aug 30 '15 at 15:42