1

I want to simulate a specific case of the UNIX rename() function to rename directories. Its MAN page specifies:

oldpath can specify a directory. In this case, newpath must either not exist, or it must specify an empty directory.

I want to simulate the last case:

newpath exists and is empty.

So I have created the directories foo_dir and bar_dir and called MoveFileEx() in order to rename foo_dir to bar_dir. Here is the code without error management:

mkdir("foo_dir");
mkdir("bar_dir");
MoveFileEx("foo_dir", "bar_dir", MOVEFILE_REPLACE_EXISTING)

But MoveFileEx() always fails with error 5 (access denied). I have tried with other flags for MoveFileEx() without success.

Must I manually remove bar_dir if it exists and is empty before calling MoveFileEx()? Or is there another solution?

I have not tried ReplaceFile() yet.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
vtorri
  • 166
  • 13
  • Remove the directory, and then call `MoveFile`. – David Heffernan Nov 07 '17 at 14:00
  • At a low level in the filesystem, setting the [`FileRenameInformation`](https://msdn.microsoft.com/en-us/library/ff540344) fails when the target is a directory, a read-only file, or a currently executing file. For example, we see this in the sample fastfat driver in [`FatSetRenameInfo`](https://github.com/Microsoft/Windows-driver-samples/blob/master/filesys/fastfat/fileinfo.c#L3306). NTFS fails with `STATUS_ACCESS_DENIED` in this case, but FAT fails with `STATUS_OBJECT_NAME_COLLISION`, which becomes WinAPI `ERROR_ALREADY_EXISTS`. – Eryk Sun Nov 07 '17 at 15:29
  • @David ok, thank you – vtorri Nov 08 '17 at 08:26

0 Answers0