How to cancel deferred MoveFileEx operation?
Asked Answered
I

2

8

I use the below command to delete some files after reboot the machine:

MoveFileEx(PChar(File_Address), Nil, MOVEFILE_DELAY_UNTIL_REBOOT);

How can i cancel execution of this command and prevent files from deleting after reboot?

Impracticable answered 15/10, 2011 at 12:40 Comment(2)
There appears to be no supported way to do this. What is driving this request?Supralapsarian
The easiest way is to not call MoveFileEx to delete the file in the first place until you're absolutely sure you want to delete it.Sato
F
14

Files you enqueue for deletion this way are placed in the registry under HKLM\System\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations. Perhaps you can delete entries from there, to prevent the files from being deleted. I couldn't find an API function for this.

Farro answered 15/10, 2011 at 12:50 Comment(1)
+1. You can delete the entry from that registry key using Delphi's TRegistry class or the API directly, but note that on Vista and above you'll need to be logged in as Administrator to do so (normal users can't write/delete from HKLM). The better solution, of course, is to not create the entry in the first place until you're absolutely certain you want the file deleted. :)Sato
O
1

I guess you could copy the file (since it hasn't been deleted yet) and then use

MoveFileEx(copy_of_file, original_file, MOVEFILE_DELAY_UNTIL_REBOOT)

to put it back in place during the reboot.

As Ken White has pointed out, though, it would be much much better to avoid this situation in the first place.

Octastyle answered 16/10, 2011 at 0:33 Comment(2)
It is indeed documented. "The move and deletion operations are carried out at boot time in the same order that they are specified in the calling application. "Farro
@CodeCaster, thanks, and well spotted. I've edited accordingly.Octastyle

© 2022 - 2024 — McMap. All rights reserved.