0

I am using MoveFile for WinAPI in my C code to rename a file. The file is renamed to the same parent directory in the same volume. But, intermittently I see MoveFile keeping the existing original file and creating the new renamed file as well. Hence, we have both the files.

The exit code returned is non zero which denotes operation was successful

Pseudo Code snippet for better referrence.

int
file_move(wchar_t *existing_file, wchar_t *dest_file)
{
    if (exists(dest_file) == 0) {
        rc = MoveFile(existing_file, dest_file);
        if (rc == 0) {
           rc = GetLastError();
           printf("File could not be renamed with error %u", rc);
        }
        printf("File was renamed.");
    }
}

exists checks whether a file path is present or not.

Ideally, the original file should not be present and only the renamed file should be kept.

The issue can be reported because the original files, if present are moved to different location on completion of renaming. We can also see the renamed files based on logs.

Files with same pattern name but just different name e.g. file_2_name.sqlite files are renamed properly.

The renaming is done to change the extension of the file. The return value was also non-zero signifying no errors.

  • Could be a case sensitivity issue. While filename lookup in Windows is case insensitive, NTFS is perfectly capable of storing files in a case sensitive manner. That's the only cause I can think of for this to happen. – *Either way, we need to see your code!* – datenwolf May 27 '22 at 09:00
  • Perhaps post the code that uses `MoveFile` for more clarity and how you determined "I see MoveFile keeping the existing original file and creating the new renamed file as well". – chux - Reinstate Monica May 27 '22 at 09:19
  • I have added the code for better referrence. – Pratyush Rath May 27 '22 at 10:39
  • 1
    I would try to diagnose the problem with [ProcMon](https://learn.microsoft.com/en-us/sysinternals/downloads/procmon). Use only the file filter and set only your process name to be filtered (see the `filter` menu in procmon). You should see the error when the system tries to delete the old file and fails to do so. The exact error code should tell you exactly what's going wrong. – Neitsa May 27 '22 at 17:09
  • @PratyushRath: what is the error code you actually observe in case of failure to rename? – chqrlie May 27 '22 at 17:16
  • The return value was non-zero. – Pratyush Rath May 30 '22 at 10:40
  • Maybe application that calls MoveFile only has read permission to that file so the copy part of move worked but remove didn't. Or the file is opened by another process and it cant be removed. – Zuljin May 31 '22 at 08:10

0 Answers0