Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 230
Armin Taghavizad Avatar asked Mar 30 '11 18:03

Armin Taghavizad


People also ask

How do I automatically delete files after a certain time?

You can free up space and keep things organized by only deleting files that are older than a certain number of days in any folder — here's how to do it. To delete files older than 30 days on Windows 10, use the “ForFiles” command. The command is: ForFiles /p “C:\path\to\folder” /s /d -30 /c “cmd /c del /q @file”.

What happens when a file is deleted while a process is writing data to the same file on Linux?

The actual object/file will be removed later by a GC when all references are gone.

What happens when a file is deleted while a process is writing?

If you delete it while it is written depending on the writing method, it will be either recreated with new data or space will continue to be written but the file won't be accessible. Third case, the file is written/closed on each new data block so then you will get "file not found" or other type of errors.

Why is Windows so slow deleting files?

The reason is that during the delete process, Windows 11/10 needs to run calculations, analyze, and show updates as files and folders are deleted, something that usually takes long time when deleting thousands of files and folders.


3 Answers

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);
like image 180
Ken White Avatar answered Oct 14 '22 09:10

Ken White


Use cmd.exe instead. That's the "new" command prompt since Windows NT.

cmd.exe /c del "c:\some file.ext"
like image 24
R. Martinho Fernandes Avatar answered Oct 14 '22 08:10

R. Martinho Fernandes


Just a guess: Looks like you are running "DOS" command.com that works with short file names only. If you are on Win2K and later, use cmd.exe instead of command.com and yes, use double-quotes.

like image 25
Eugene Mayevski 'Callback Avatar answered Oct 14 '22 09:10

Eugene Mayevski 'Callback