0

I've been looking for the most convenient way to tell Windows 10 to delete certain files upon rebooting. Having a right-click context menu option seems to be the most efficient solution. I've stumbled across the following registry entries that give me such an option, but on Windows 10 x64 it isn't actually deleting the files I apply it to.

[HKEY_CLASSES_ROOT\*\shell\Delete on reboot\command]
@="CMD /E:OFF /C REG ADD HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\Currentversion\\RunOnce /v \"Del %1 OnNextReboot\" /d ^\"cmd.exe /c DEL /F /Q \\\"%1\\\"\" /f\""

[HKEY_CLASSES_ROOT\*\shell\Open]

[HKEY_CLASSES_ROOT\Folder\shell\Delete on reboot\command]
@="CMD /E:OFF /C REG ADD HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\Currentversion\\RunOnce /v \"Del %1 OnNextReboot\" /d ^\"cmd.exe /c RD /S /Q \\\"%1\\\"\" /f\""

Apparently this registry code has been floating around the internet for a long time, and the most updated information I could find about it was someone saying that it worked for them on Windows 7. Is there some way I can modify it to work on Windows 10?

Thank you.

2

My solution was made possible through w32sh's help. The solution I'm sharing in this answer is after following his instructions first.

In the end, what I ended up doing here was combining a command for taking ownership of the file and using movefile.exe on it.

I created a batch file (I called it "movefile_custom.bat") with the following:

takeown /f %1 /r /d y && icacls %1 /grant administrators:F /t
takeown /f %1 && icacls %1 /grant administrators:F
"C:\Windows\movefile.exe" %1 ""

(NOTE: The right-click entry only works on files, so the first line that handles directories is probably unnecessary, but I left it in for now anyway. It doesn't hurt anything)

I then downloaded BAT to EXE Converter and used it to (you guessed it) convert my "movefile_custom.bat" to "movefile_custom.exe". The converter has an administrator option for the EXE.

I edited the registry entry to point to my new "movefile_custom.exe" file instead of the base "movefile.exe".

Works great now.

1

Download SysInternals Movefile.exe and place it in C:\Windows folder.

Right-click Movefile.exe and click Properties. Select the Compatibility tab, click "Change settings for all users" Enable "Run this program as an administrator" Click OK, OK.

Then create a .REG file as below.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\deleteonreboot]
@="Delete File on Reboot"

[HKEY_CLASSES_ROOT\*\shell\deleteonreboot\command]
@="\"C:\\Windows\\movefile.exe\" \"%1\" \"\""

Double-click the REG file to apply the settings.

This adds Delete on reboot option in the right-click menu.

  • Thank you! Unfortunately this isn't working. Everything gets entered into the registry just fine, and I know that the movefile.exe is being executed when I select the right-click option, but the files just aren't being deleted after rebooting. Actually, let me try manually using movefile.exe on a file and see if that works... I'll report back asap. – vertigoelectric Sep 7 '16 at 14:47
  • Ok. Let's know how it goes.. It worked fine here when I tested. – w32sh Sep 7 '16 at 14:53
  • It's not deleting the files. However, now that I understand how to use the registry to apply basically any executable/script to a file using right-click, perhaps I can use something other than movefile.exe to do this... but still... I'd like to figure out why it's not working. – vertigoelectric Sep 7 '16 at 14:56
  • Ah, in my test batch file I've added "pause" and now see that I'm getting errors. I'm testing two files, one of them has a special character. The normal file returns "Error: 5", the other file returns "Error: 123". I'm going to research these. – vertigoelectric Sep 7 '16 at 15:00
  • Perhaps the file you're trying to delete is owned by "TrustedInstaller"? – w32sh Sep 7 '16 at 15:00
0

This page describes how to use Group Policy to run custom scripts on shutdown/reboot, or logoff: https://technet.microsoft.com/en-us/library/cc753404(v=ws.11).aspx

I've used it in the past on previous versions of Windows, and although the page says only up to Win 7/2012, I know that similar policies also work on 10. I'd wager this would too.

There are also third-party services that you can configure to run a script on shutdown/reboot - such as SRVSTART (http://www.rozanski.org.uk/services), although it's not very straightforward in that context and not it's primary purpose.

  • Thanks for the input. I do know there are various ways to automate scripts on shutdown/reboot, but I'm not quite sure how I might implement the "Delete on Reboot" right click item into those. – vertigoelectric Sep 3 '16 at 3:45
  • @vertigoelectric Sorry, I misunderstood your question, my bad. – Jim Sep 3 '16 at 21:54
  • @w32sh, Thanks! Yeah, if you could help me with getting that added to the menu I'd be very grateful. Be sure to post it as an answer so I can select it. – vertigoelectric Sep 4 '16 at 13:00
  • @bubbles, no worries! – vertigoelectric Sep 4 '16 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.