Win32 API for rename a file in C

tom picture tom · Dec 22, 2010 · Viewed 19.2k times · Source

If the source directory and the target directory, MoveFile would actually make a copy of the source file into the target file, which means that I will end up seeing two files.

Is that the best way that rename can be achieved?

Answer

Greg Hewgill picture Greg Hewgill · Dec 22, 2010

The MoveFile function is indeed what you want. From the documentation:

The MoveFile function will move (rename) either a file or a directory (including its children) either in the same directory or across directories.

If the source and destination locations are both on the same volume, then an atomic rename operation is performed. If they're on different volumes, then a copy/delete operation is done instead (this is the best you can do).