How to mark your file for deletion after next reboot?

19 05 2008

You might have seen, while un-installing some applications, the uninstaller says – “Some files remains and will be removed after next reboot”. How they can mark a file to be deleted after next restart?

The answer is MoveFileEx(). The MoveFileEx() have a special flag MOVEFILE_DELAY_UNTIL_REBOOT. By specifying it, the file will be marked for deletion during the next reboot. See the sample code snippet.

void MarkFileForDeletion(CString csFileName )
    MoveFileEx( csFileName, // Source File.
                0, // Destination as null.

Actually, the MoveFileEx(), places the filename pair in the registry at – HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations. The PendingFileRenameOperations is of type REG_MULTI_SZ which can hold multiple strings. So that it can hold multiple pairs of source-destination filenames.

The MoveFileEx() is only applicable for Windows NT family. For Windows 95/98/Me you’ve to modify the WININIT.INI file. In that file, you’ve to modify the [rename] section. Since an old 95 box is not available, i couldn’t try it. If you have, just try and let me know!

Targeted Audience – Intermediate.