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.

7
  • 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! Commented May 27, 2022 at 9: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". Commented May 27, 2022 at 9:19
  • I have added the code for better referrence. Commented May 27, 2022 at 10:39
  • 1
    I would try to diagnose the problem with 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. Commented May 27, 2022 at 17:09
  • @PratyushRath: what is the error code you actually observe in case of failure to rename? Commented May 27, 2022 at 17:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.