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

3

6

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.

Ornamental answered 30/3, 2011 at 18:39 Comment(11)
What version of windows is this? Is this something with cmd.exe, or something pre-XP?Misgovern
@Armin: Then there is no command.com. command.com was used in Windows 9x! Since Windows XP we use cmd.exe instead!Coenosarc
@Armin, this does not like a programmer question... I don't know why you tagged this Delphi, but for me this belongs to superuser. If you think this really belongs to SO (as defined in the FAQ, please edit your question to avoid migration.Taneka
@jachguate, read the first sentence: My application needs to delete some files on windows startup". To me that means the application is writing to the RunOnce registry key, and that means it's a programming question.Downbow
@Andreas Rejbrand; Thank you, I googled that and found that. I guessed i have to use CMD.exe but preffered to use what i found. @jachguate; I dont say i'm a professorial programmer, i'm trying to be and it will be done by searching and asking. i asked here, because i thought when i'm using Delphi maybe there is a specific way DelphiOrnamental
@Armin: Your request is perfectly valid. It's the phrasing of your question that was confusing, especially because you tagged your question as delphi but never mentioned how your problem is connected with Delphi (which turned out to be the most important part of your question). So, while learning Delphi, be careful to learn how to ask questions too. :)Gendron
@Armin I did a re-write of your question, let me know if you think it expresses correctly what you're looking for, and what's already answered.Taneka
@jachguate; Absolutely its better than what i wrote! Thank youOrnamental
@Armin: Note: Even if you get RunOnce working, it will fail if the user doesn't have administrator access.Celestyna
@Gerry, there's a RunOnce in the HKCU branch of the registry, you don't need administrator privileges to write that. Of course, the commands there will run until the same user logon, not just the reboot.Taneka
@jachguate: Indeed - so the suitability of this will depend on what Armin is actually trying to achieve.Celestyna
D
30

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);
Downbow answered 30/3, 2011 at 18:48 Comment(16)
Wow. Learned a ton from this answer. +10. You overflowed my daily learning quota (which is rather low). I'll have to skip it tomorrow :)Misgovern
Thank you. it was an very usefull article. but 3 more questions: (1)I use RunOnce key because file may use with some programs or processes and in RunOnce key it will be deleted before that. Does your way work same? (2) How it differs between files and folders? maybe i have file with no extension and a folder with same name!! (3) when I used RunOnce key, when i entered many string values it only deleted some of them, i removed /c switch and i saw this error: "Too many parameters". Does your way have this issue too?Ornamental
Armin, (1) that question doesn't parse. Please rephrase it. (2) Files and folders share a namespace. It's impossible to have a file and a folder with the same name. (3) Each to-be-deleted file is stored separately in whatever place the OS chooses to store that information, so you don't need to worry about trying to delete too many files at once.Finery
@Armin: 1) MoveFileEx as I described deletes the file at startup. 2) If you're deleting a file, it's in a folder, right? If you can do it with RunOnce, you can do it (better) with MoveFileEx. 3) You do multiple files with multiple calls to MoveFileEx; they're executed one after the other. It's in the article I linked.Downbow
NOTE WELL: "This value can be used only if the process is in the context of a user who belongs to the administrators group or the LocalSystem account." Not sure how this acts if you are on Win7 or Vista with UAC enabled, but it is a severe limitation of this command. It will fail for most users on a well implemented (commercial) domain.Celestyna
(Continued afetr short research break) However, there will also be problems trying to write to HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce if you aren't in the admin group as well.Celestyna
@Gerry: If the app was able to write to RunOnce, it should have sufficient privileges to call MoveFileEx. Point taken, though; it probably should have been mentioned in my answer - I thought the link explained it well enough.Downbow
@Gerry - I posted my last comment reply to you during your short break, I guess. :)Downbow
@Ken: OK, I just wanted to state this as it seems to be a common mistake - that I nearly made myself once before noticing the caveat. Of course if the app is running as a service under a service account this isn't an issue, but could be in a GUI app, depending on the target market of the application. As I don't have access to a Win7 machine with Delphi on it, I don't know how UAC will react.Celestyna
Hey guys...... i occurred another problem! This function does not work for non empty directories if i use MOVEFILE_DELAY_UNTIL_REBOOT flag. What should i do?Ornamental
@Armin: It says in the linked article that you need to delete the files in the directory first, and then delete the directory. You do this with multiple calls to MoveFileEx. Did you read the linked page?Downbow
yes Ken, I have read it carefully and complete and when i wrote "This function does not work for non empty directories if i use MOVEFILE_DELAY_UNTIL_REBOOT flag." i said it according to the linked article. I asked it because i thought maybe there is another way, something like changing some parameters in function or something else..... Sorry Man!Ornamental
This flag didn't work if you want to delete application that is running now from itselfGasket
@se_pavel: Of course it won't. It specifically says "delay until reboot". I'm not sure what your point might be here. (RunOnce won't work to "delete an application that is running now from itself" either.Downbow
I was wrong with my prev comment - general rule: you can't use MOVEFILE_DELAY_UNTIL_REBOOT until you have admin rights, this is annoyingGasket
@se_pavel: I'm sorry. There's nothing I can do about that, I'm afraid. Commenting to me about it won't change it. :-) There's also a comment above with a NOTE WELL in bold that says admin rights are required. Please read the other comments before adding any additional ones, as this was discussed before (as was the fact that RunOnce also requires admin rights under Vista and above due to UAC).Downbow
M
2

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

cmd.exe /c del "c:\some file.ext"
Misgovern answered 30/3, 2011 at 18:45 Comment(4)
user can see the cmd window for secondsGasket
also you can use 8.3 filenames and avoid quotesGasket
@Gasket 8.3? Seriously? Urgh.Migdaliamigeon
I have try both variants cmd+del and MOVE_UNTIL_DELAY and both didn't what I need, then I decided to create scheduled task via WINAPI (#14988949 other problem), then I decided to create scheduled task with the following line Schtasks.exe /TR "cmd /c del /q <filename" and in such command line you can't use doublequotes in doublequotes so short file name (8.3) is really helpfullGasket
S
1

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.

Scotopia answered 30/3, 2011 at 18:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.