How can I delay file deletion until next reboot from my program?

delphi, delphi-7

Solution

Don't use `RunOnce`, and don't use `Command.com`. If you insist on using something, use `%COMSPEC% /c` instead. You have a better option, though.

Use MoveFileEx with the `MOVEFILE_DELAY_UNTIL_REBOOT` flag instead:

if not MoveFileEx(PChar(YourFileToDelete), nil, MOVEFILE_DELAY_UNTIL_REBOOT) then
  SysErrorMessage(GetLastError);

Problem

My application needs to delete some files, but this should happen until next windows startup. What I'm doing now is to write this string value in RunOnce registry key: ``` Command.com /c del c:\some file.ext ``` but I observe a problem with paths with embedded spaces. I have to say I tried this one too: ``` Command.com /c del "c:\some file.ext" ``` But this does not resolve the problem, but make it worst: not deletion of any file, regardless of embedded spaces! What is the correct way to delete files from my program delayed to the next reboot? Thanks.

Original source