auto delete script after 7 days ? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Milos Zivkovic
Posts: 4
Joined: 28 Jan 2024, 07:05

Re: auto delete script after 7 days ?

29 Jan 2024, 03:58

maybe i dont know to explain, when i run ahk script with original code process stops, tray icon dissapears and ahk script file deletes itself. When i run compiled file only evrything same except compiled file dont delete itself.
User avatar
mikeyww
Posts: 27143
Joined: 09 Sep 2014, 18:38

Re: auto delete script after 7 days ?

29 Jan 2024, 06:49

I will write my last post differently in case it helps. An uncompiled script does not (itself) run as a process, but a compiled script does. I am not aware of a way to delete the file of a running process. You can have a different process kill the process first (or wait for the first process to close) and then delete its file. Others here may know other ways.

Your second process could be an uncompiled script that does delete itself before it exits.

Code: Select all

; This script deletes a program when the program closes. The script then deletes itself and exits.
#Requires AutoHotkey v1.1.33
program := "test.exe"                             ; Process whose file should be deleted
DetectHiddenWindows On
Process Wait, % program                           ; Wait for the program to run
SoundBeep 1500
WinGet progPath, ProcessPath, % "ahk_exe" program ; Get the program's path
MsgBox 64, Program path, % progPath
Process WaitClose, % program                      ; Wait for the program to exit
delFile(progPath)                                 ; Delete the program
delFile(A_ScriptFullPath)                         ; Delete this script file
MsgBox 64, Status, Exiting!
ExitApp

F4::Process Close, % program                      ; F4 = Close the program

delFile(filePath) {
 FileRecycle % filePath
 If ErrorLevel
  MsgBox 48, Failure, % "An error occurred when deleting the file.`n`n" filePath
}
TAC109
Posts: 1118
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: auto delete script after 7 days ?

29 Jan 2024, 23:01

You can use this code in your main script after it has decided that it is time to delete the script or .exe file:

Code: Select all

MsgBox The compiled .exe              ; For testing

                                      ; Must be compiled with an .exe base file
;@Ahk2Exe-Base ..\AutoHotkeyU32.exe, %A_ScriptName~\.[^\.]+$%_1

if A_IsCompiled                       ; Code to delete the running .exe
{	RunBase := RegExReplace(A_ScriptFullPath, "\.[^\.]+$")

	RunTxt = DetectHiddenWindows On`n WinKill      ahk_id %A_ScriptHwnd%`n
	RunTxt = %RunTxt% #NoTrayIcon  `n WinWaitClose ahk_id %A_ScriptHwnd%,,10
	
	FileDelete           %RunBase%.ahk
	FileAppend %RunTxt%, %RunBase%.ahk

	RunTxt = "%A_ScriptFullPath%" /Script "%RunBase%.ahk" & del "%RunBase%.*"
	RunWait "%ComSpec%" /c "%RunTxt%",, Hide
	
} else FileDelete %A_ScriptFullPath%  ; If not an .exe, delete the script.
Note that the script must be compiled with an .exe base file. I have used the 'Base' compiler directive to select 'AutoHotkeyU32.exe' to use as the base file for this test. In the same directive I have ensured that the compiled .exe has a different name to the source so that the test source script is not deleted when testing!

The directory that contains the compiled script must be writable for this method to work, as it temporarily creates a small script file in the same directory.

With this method it is not required for AutoHotkey to be installed on the machine that is running the compiled .exe.

You can save and compile the above script and run the compiled .exe to see how it works.

Cheers
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 96 guests