0

I am having a problem moving a directory to a new location, specifically one I create with CreateDirectory. Here is the code:

if (FALSE == CreateDirectory(strDestination, NULL))
{
    dwError = GetLastError();
    if (ERROR_ALREADY_EXISTS != dwError)
    {
        strError.Format("Error creating %s: %i", strDestination, dwError);
        LogIt(strError);
    }
}
if (FALSE == MoveFile(strSource, strDestination + strID))
{
    dwError = GetLastError();
    strError.Format("Error moving %s to %s: %i", strSource, strDestination + strID, dwError);
    LogIt(strError);
}

However, if I manually create a directory, I am able to feed that path into this code and it works. I have compared the security settings for these two directories, and made sure they were the same, but it's still not working. Is there something I'm doing wrong with my creation code? If I leave the second parameter as NULL, shouldn't it grant the same permissions it would when I manually create the directory?

Joe M
  • 3,060
  • 3
  • 40
  • 63
  • They're both type `CString`. – Joe M Nov 12 '12 at 22:16
  • 2
    Is the source and destination on the same drive in both your success case and your failure case? – Josh Heitzman Nov 12 '12 at 22:20
  • Aha, good catch. The source is on `C:`. The success was on the same drive, but the failure was on `E:`. I tried it with a manually created directory on `E:` and it failed in the same way. I didn't know files couldn't be moved across drives. – Joe M Nov 12 '12 at 22:38
  • 2
    Files can move across drives, but directories cannot. [Read the documentation](http://msdn.microsoft.com/en-us/library/windows/desktop/aa365239.aspx): "The one caveat is that the MoveFile function will fail on directory moves when the destination is on a different volume. If a file is moved across volumes, MoveFile does not move the security descriptor with the file. The file will be assigned the default security descriptor in the destination directory." – Remy Lebeau Nov 12 '12 at 22:49
  • I had planned to switch my code over to use `CopyFile` and go through an array of all the files in the directory, then delete afterward. However, I found out that the testing scenario doesn't match the way it will be run in the field, where the source and destination will both be on the same drive, so my original code will work. Thanks to everyone for the information and ideas though! – Joe M Nov 13 '12 at 18:00

1 Answers1

4

If you're running antivirus on the machine, it could be locking the new folder while it verifies it/adds it to its clean cache. That could cause intermittent timing issues if, for example, the AV driver is bogged down with other activity.

To check that this is what's happening you could disable your AV's on-access scanner. A workaround in your code would be to retry (say) 2-3 times with a small delay between.

Edit: Since the OP has confirmed that it's failing moving to a different volume, the answer is to use MoveFileEx() with the MOVEFILE_COPY_ALLOWED flag.

HerrJoebob
  • 2,264
  • 16
  • 25