0

I'm using CopyFileEx to copy files with that ability. Is there anything like it for moving files? Remember that not every move is fast – moving to a different drive is actually a copy.

The solution I have now is to copy-and-delete-the-original if the move is to a different root directory, and to simply move (and hope it's really just a change in the pointer to the file) if it's to the same root directory.

Is there a straightforward solution (like CopyFileEx)?

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
ispiro
  • 26,556
  • 38
  • 136
  • 291

3 Answers3

3

MoveFileTransacted or MoveFileWithProgress might be more suitable? I believe that can be cancelled.

BOOL WINAPI MoveFileWithProgress(
  __in      LPCTSTR lpExistingFileName,
  __in_opt  LPCTSTR lpNewFileName,
  __in_opt  LPPROGRESS_ROUTINE lpProgressRoutine,
  __in_opt  LPVOID lpData,
  __in      DWORD dwFlags
);

When moving a file across volumes, if lpProgressRoutine returns PROGRESS_CANCEL due to the user canceling the operation, MoveFileWithProgress will return zero and GetLastError will return ERROR_REQUEST_ABORTED. The existing file is left intact.

When moving a file across volumes, if lpProgressRoutine returns PROGRESS_STOP due to the user stopping the operation, MoveFileWithProgress will return zero and GetLastError will return ERROR_REQUEST_ABORTED. The existing file is left intact.

Cylindric
  • 5,858
  • 5
  • 46
  • 68
  • Thanks. What is the difference between them = what does "...as a transacted operation" mean? – ispiro Dec 08 '11 at 14:25
  • 1
    If the move is for a whole directory for example, they either ALL succeed, or none of them do. I believe that only applies to local NTFS moves though, not network moves. – Cylindric Dec 08 '11 at 14:31
  • Transacted means it's atomic. All or none. – Bengie Dec 08 '11 at 14:32
  • @Cylindric: Depends on your version of Windows. With 7/2K8R2, you can have cross system transactions. You can actually link an SQL transaction with local filesystem and remote filesystem transactions. I've only read about the feature, not actually used it. – Bengie Dec 08 '11 at 14:34
  • That's bonkers :) There's some clever stuff coming down the line. – Cylindric Dec 08 '11 at 14:37
  • Win7/2K8R2 are still not mainstream enough, so I wouldn't target that feature :P – Bengie Dec 08 '11 at 14:44
2

Use MoveFileWithProgress and return PROGRESS_CANCEL from the lpProgressRoutine.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
1

I would use the SHFileOperation API, see here for a .NET C# wrapper:

https://stackoverflow.com/a/3282481/559144

and also refer here: http://pinvoke.net/default.aspx/shell32.SHFileOperation

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147