Files that are currently opened or in use in Windows cannot be moved or deleted until they are closed. Most of the time closing the associated program would do the job but sometimes it still won’t work because its mapped to the memory or protected by another process. To solve this problem, most of us would simply rely on a third party software such as FileASSASSIN, LockHunter, IObit Unlocker, BlitzBlank and etc that is capable of detecting locked files and attempt to unlock them for further actions. Unlocking locked files has its own risk that can possibly cause crashes on the operating system.
The safer solution is to mark the file for deletion on the next reboot and the Microsoft Windows operating system has made it easy for developers because it comes with a MoveFileEx API to delete or rename/move files the next time the system boots up. This task is performed by reading a registry entry called PendingFileRenameOperations located at HKLM\SYSTEM\CurrentControlSet\Control\Session Manager. Do note that the PendingFileRenameOperations registry key will not exist unless there is a pending operation.
If you do not like messing with the Windows registry, here we have a few free tools that can help you read the PendingFileRenameOperations keys and display the list of pending file operations. Some of them have the capability of allowing you to schedule move and delete commands for the next reboot.
PendMoves is a simple command line application by Windows Sysinternals where it can read the PendingFileRenameOperations keys and display the list of files that will be either deleted or moved to the new target location. Empty target will be shown as DELETE or else it will be the new location where the source file will be moved to.
Since PendMoves is a command line application, it must be run from the command prompt in order to see the results or else you’ll only get a flashing black window opening and closing by itself. Press WIN+R, type cmd in the Run box and click OK. Change to the directory where pendmoves.exe is located and type the filename to run the application.
A separate application called MoveFile that is bundled together with PendMoves in the archive is for adding an entry in PendingFileRenameOperations to schedule a file to be deleted or moved on the next Windows boot. Simply specify the source and destination to move the file or leaving the destination empty with two double quotes will delete the source.
The example command below will move the test.zip from C:\ to the Raymond user account’s desktop.
C:\movefile.exe c:\test.zip c:\raymond\user\desktop\test.zip
The command below will schedule to delete the test.zip file at next Windows boot.
C:\movefile.exe c:\test.zip “”
If you prefer a more user friendly tool that has a graphical user interface, WhyReboot would be your choice. WhyReboot is a small and portable tool that displays a list of pending file operations and additionally the RunOnce registry key. The RunOnce registry key is an instruction for Windows to automatically run a specified program only once during startup and the key will be removed when it has processed the request.
Although WhyReboot is created to determine if you should restart your computer when you’re prompted to after installing a program or update, it is still quite far from being able to do that. WhyReboot is not capable of telling you if an important core driver or DLL file has been replaced and requires a restart to load which is quite commonly seen in security based software such as an antivirus program.
3. WMI VBScript
For administrators or programmers who prefer scripting for flexibility in implementing automation, we found a handy WMI VBScript written by Tom Mills, a Solution Architect with Microsoft Services. Basically what the VBS file does is read and parse the PendingFileRenameOperations showing the result in a popup window.
The VBS code can be obtained from the official Tom Mills MSDN blog site or you can download the script below.
There are two versions of MoveEx, one is created by Alexander Frink which can be found in Softpedia, the other by Frank Westlake from ss64.net and we’re featuring the latter. The reason why we prefer MoveEx by Frank over Alexander is the ability to delete an individual entry in PendingFileRenameOperations other than just parsing and displaying the operation.
To view the PendingFileRenameOperations entries, simply run the application on the command line. To delete an individual PendingFileRenameOperations entry, you’ll need to append /d together with the entry number that is shown when running MoveEx without a command line argument. As you can see from the screenshot above, the first command list all the entries in PendingFileRenameOperations and the second command that has a “/d 003” deletes the third entry.
Adding a PendingFileRenameOperations entry using MoveEx is the same as MoveFile from Sysinternals which is specifying the source followed by target. Leaving the target empty will schedule Windows to delete the source file.
5. Manually Add or Delete PendingFileRenameOperations Entry
There’s no problem in manually deleting the PendingFileRenameOperations key from the system registry but adding an entry is tricky because it is a REG_MULTI_SZ value not the normal string or DWORD. To delete, simply navigate to HKLM\SYSTEM\CurrentControlSet\Control\Session Manager in registry and delete the PendingFileRenameOperations key.
To add a delete entry, go to HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\ right click at the right pane, go to New, select Multi-String Value and name it as PendingFileRenameOperations. Double click on the newly created registry value and enter the full location of the file starting with \??\ (for example, \??\C:\raymondcc.exe) that you want to schedule for deletion. Click OK when you’re done. Now right click at PendingFileRenameOperations and select Modify Binary Data. You should see a bunch of numbers in the value data. Enter four zeros “00 00” without the quotes at the end of the hex string and click OK. Restart your computer and the file will be deleted.
Weirdly, scheduling a move operation on a file during Windows boot using PendingFileRenameOperations is so much easier if compared to deleting because it does not involve modifying the binary data in the registry. Follow the steps above until you’ve reached the part to add a value data in Edit Multi-String box. You need to specify two paths which is the source and target. An example below will move the file raymondcc.exe from C:\source to c:\target.