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.
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”.
The actual object/file will be removed later by a GC when all references are gone.
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.
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.
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);
Use cmd.exe instead. That's the "new" command prompt since Windows NT.
cmd.exe /c del "c:\some file.ext"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With