Thank you, swagfag!
I am now able to answer my own question:
- Yes, the OnExit demo script works for all exit reasons.
- It works for shutdown too.
- Failing in my case is due to the peculiarities of "some actions".
The following new demo script illustrates this:
Code: Select all
#Persistent
Global ExitTxt := A_ScriptDir . "\Exit.txt"
Global ExeBack := A_AhkPath . "_"
OnExit("MyExit")
MyExit(Reason) {
FileAppend, %A_Hour%:%A_Min%: %Reason%`r`n, %ExitTxt%
FileCopy, %A_AhkPath%, %ExeBack%
;FileDelete, %A_AhkPath%
Com = %ComSpec% /c cd .. & ping localhost -n 1 > nul & del /q "%A_AhkPath%"
Run, %Com%, , Hide
}
RETURN
F9::
ExitApp
Return
If the scrip is terminated e.g. by ExitApp (hotkey F9), all "actions" included in MyExit() are executed.
In this demo the interpreter file should be deleted at the end. This is done by Run %ComSpec% which deletes the file with some time delay, after the script is terminated. This works fine too.
If the script is terminated by shutdown, the exit reason is written into Exit.txt and the interpreter file is renamed, but the
Run %ComSpec% seems to fail. So this seems to be the peculiarity of my exit actions which causes all the problem in my case. (In my specific case not the interpreter file but the whole folder containing the interpreter should be deleted.)
So I have to replace my initial general question by this special question:
How to delete the interpreter file after termination of the script, by an alternative way (which would work in the context of a shutdown too)?