1

I’m working on a console program and need to use MoveFile/CopyFile to allow moving and copying files and directories (possibly across volumes). The problem of course is that copying or moving a directory to another volume does not work with the aforementioned functions because they are not recursive.

SHFileOperation will not do because this is a console app and I am using the variations that allow for progress display (MoveFileWithProgress/CopyFileEx), and SHFileOperation uses the GUI for displaying progress instead of the console.

I considered using FindNextFile, but even then I could not find any examples of code to recursively (Move|Copy)File with FindNextFile or otherwise—which is kind of baffling since this issue must have come up before.

Is there an easy way to do this or do I have to resort to reinventing the wheel?

Synetech
  • 9,643
  • 9
  • 64
  • 96
  • Are you sure they aren't recursive? MSDN clearly states that you may give a path of a directory, and that `The MoveFile function will move (rename) either a file or a directory (including its children)`. Note however this disclaimer: `If a file is moved across volumes, MoveFile does not move the security descriptor with the file` (also in MSDN). – Eran Zimmerman Gonen Aug 07 '12 at 18:23
  • @Eran, it specifically says *the [MoveFile](http://msdn.microsoft.com/en-us/library/windows/desktop/aa365239.aspx) function will fail on directory moves when the destination is on a different volume.* – Synetech Aug 07 '12 at 19:19
  • of course, I misread it... I googled it a bit, and it seems that people say that moving doesn't work, but copying & deleting does, but I didn't find actual explanations of how to copy. Seems you should just write a recursive function yourself (or take one from the web - plenty of those around). Another way (not as good, IMO, but it's still possible) is to read and write the whole folder, somewhat like what's explained here: http://stackoverflow.com/questions/5520481/move-folder-cross-disks-without-security-descriptor-change – Eran Zimmerman Gonen Aug 07 '12 at 21:07
  • @Eran, writing a recursive function is possible but annoying since it has to use FindNextFile and such and seems “kludgy”. I’m not opposed to it (maybe I can add more flexibility that way), but I just figured that there must surely be some easier, built-in way to do such an obvious, and relatively common task instead of just reinventing th wheel, which is a real anathema in the programming world. – Synetech Aug 07 '12 at 21:20

0 Answers0