auto delete script after 7 days ? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wrldestro
Posts: 35
Joined: 06 Oct 2020, 17:28

auto delete script after 7 days ?

Post by wrldestro » 27 Nov 2020, 19:07

Is it possible to auto delete script after 7 days ?
1. Someone open script A.
2. Then it makes funcion in task scheduler or otherwise.
3. automatic deleted after 7 days.
4. script should work hide that user not see action.

FileDelete, ScriptName.ahk ?

User avatar
mikeyww
Posts: 27140
Joined: 09 Sep 2014, 18:38

Re: auto delete script after 7 days ?  Topic is solved

Post by mikeyww » 27 Nov 2020, 20:58

Code: Select all

; Self-deleting script
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=83736
#Persistent
; #NoTrayIcon
Process, Priority,, BelowNormal
RegWrite ; Run this script on computer startup
 , REG_SZ, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
 , Self, "%A_AhkPath%" "%A_ScriptFullPath%"
mark = %TEMP%\mark.tmp
If FileExist(mark)                         ; Initial run time was already marked, so...
 FileRead, then, %mark%                    ;  ... get the initial run time;
Else FileAppend, % (then := A_Now), %mark% ;  otherwise, now is the initial run time.
SetTimer, Check, 3600000                   ; Check for expiration hourly
Check:
; SoundBeep, 1500, 20
dif := A_Now
dif -= then, Days ; Days since initial run time
If (dif < 7)      ; Script remains valid
 Return
; Script expired
; MsgBox, 48, Auf wiedersehen! (%dif%), Until we meet again!
FileRecycle, %A_ScriptFullPath% ; Delete this script
FileRecycle, %mark%             ; Delete the mark file
ExitApp

hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: auto delete script after 7 days ?

Post by hasantr » 28 Nov 2020, 02:17

mikeyww wrote:
27 Nov 2020, 20:58

Code: Select all

; Self-deleting script
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=83736
#Persistent
; #NoTrayIcon
Process, Priority,, BelowNormal
RegWrite ; Run this script on computer startup
 , REG_SZ, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
 , Self, "%A_AhkPath%" "%A_ScriptFullPath%"
mark = %TEMP%\mark.tmp
If FileExist(mark)                         ; Initial run time was already marked, so...
 FileRead, then, %mark%                    ;  ... get the initial run time;
Else FileAppend, % (then := A_Now), %mark% ;  otherwise, now is the initial run time.
SetTimer, Check, 3600000                   ; Check for expiration hourly
Check:
; SoundBeep, 1500, 20
dif := A_Now
dif -= then, Days ; Days since initial run time
If (dif < 7)      ; Script remains valid
 Return
; Script expired
; MsgBox, 48, Auf wiedersehen! (%dif%), Until we meet again!
FileRecycle, %A_ScriptFullPath% ; Delete this script
FileRecycle, %mark%             ; Delete the mark file
ExitApp
Nice solution. Congratulations.

wrldestro
Posts: 35
Joined: 06 Oct 2020, 17:28

Re: auto delete script after 7 days ?

Post by wrldestro » 28 Nov 2020, 06:55

yea he is soo good

User avatar
mikeyww
Posts: 27140
Joined: 09 Sep 2014, 18:38

Re: auto delete script after 7 days ?

Post by mikeyww » 28 Nov 2020, 07:02

Thank you! :D

I considered Task Scheduler, but it seemed like it would be slightly more difficult or longer.

I did not test the RegWrite effect here, but it seems like it should work, though I'm not sure when Windows deletes the registry value in relation to running the program. For example, if it runs the program first and then deletes the registry value (e.g., within a second or two), then this script might not work, though it could likely be adjusted to work simply by delaying the RegWrite.

Nothing is foolproof, of course. Someone could delete the process, the mark file, or the registry key. Nonetheless, a typical user might never notice or find this.

wrldestro
Posts: 35
Joined: 06 Oct 2020, 17:28

Re: auto delete script after 7 days ?

Post by wrldestro » 29 Nov 2020, 07:57

My script name is : SCRIPT1
Whats should i change in this code to make it work correctly and automatic delete after 7 days?

Code: Select all

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%
SetMouseDelay, -1
SetKeyDelay, -1
#Persistent
#NoTrayIcon

Process, Priority,, BelowNormal
RegWrite ; Run this script on computer startup
 , REG_SZ, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
 , Self, "%A_AhkPath%" "%A_ScriptFullPath%"
mark = %TEMP%\mark.tmp
If FileExist(mark)                         ; Initial run time was already marked, so...
 FileRead, then, %mark%                    ;  ... get the initial run time;
Else FileAppend, % (then := A_Now), %mark% ;  otherwise, now is the initial run time.
SetTimer, Check, 3600000                   ; Check for expiration hourly
Check:
; SoundBeep, 1500, 20
dif := A_Now
dif -= then, Days ; Days since initial run time
If (dif < 7)      ; Script remains valid
 Return
; Script expired
; MsgBox, 48, Auf wiedersehen! (%dif%), Until we meet again!
FileRecycle, %A_ScriptFullPath% ; Delete this script
FileRecycle, %mark%             ; Delete the mark file
ExitApp

; SCRIPT1

RClick := 1
LClick := 1
QSCOPE := 1

#If LClick
~*$LButton::
SetTimer, Dll_12, 17
SetTimer, Stop, -1200
running := True
KeyWait, LButton
If running
Gosub, Stop
Return

Dll_12:
{
DllCall("mouse_event", uint, 1, int, 0, int, 2, uint, 0, int, 0)
}
Return

Stop:
SetTimer, Dll_12, Off
running := False
Sleep, 150
Return

#If RClick
~*$RButton::
while GetKeyState("RButton","P")
{
Send, {LShift DOWN}
Sleep, 80
Send, {LShift UP}
}
Sleep 1400
Return

#If QSCOPE
~*$e::
MouseClick, right, , , 1
Send, {LCtrl down}
DllCall("mouse_event", uint, 1, int, 0, int, -5, uint, 0, int, 0)
Sleep, 30
MouseClick, left, , , 1
Send, {3}
Send, {1}
Sleep, 10
Send, {LCtrl up}
Sleep 1400
Return

#If

F9::
RClick := !RClick
SoundBeep, 1200,% 20*!RClick
return

F10::
LClick := !LClick
SoundBeep, 1200,% 20*!LClick
Return

F11::
QSCOPE := !QSCOPE
SoundBeep, 1200,% 20*!QSCOPE
Return

~Delete::ExitApp
~End::Suspend, toggle

User avatar
mikeyww
Posts: 27140
Joined: 09 Sep 2014, 18:38

Re: auto delete script after 7 days ?

Post by mikeyww » 29 Nov 2020, 08:52

You would move your earlier auto-execute section to the top, since these lines will no longer execute in their current position.

Code: Select all

RClick := 1
LClick := 1
QSCOPE := 1
I'm not seeing any reason for #Persistent in your script.

Prazul
Posts: 94
Joined: 05 Aug 2021, 15:55

Re: auto delete script after 7 days ?

Post by Prazul » 31 Aug 2021, 01:09

mikeyww wrote:
27 Nov 2020, 20:58

Code: Select all

; Self-deleting script
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=83736
#Persistent
; #NoTrayIcon
Process, Priority,, BelowNormal
RegWrite ; Run this script on computer startup
 , REG_SZ, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
 , Self, "%A_AhkPath%" "%A_ScriptFullPath%"
mark = %TEMP%\mark.tmp
If FileExist(mark)                         ; Initial run time was already marked, so...
 FileRead, then, %mark%                    ;  ... get the initial run time;
Else FileAppend, % (then := A_Now), %mark% ;  otherwise, now is the initial run time.
SetTimer, Check, 3600000                   ; Check for expiration hourly
Check:
; SoundBeep, 1500, 20
dif := A_Now
dif -= then, Days ; Days since initial run time
If (dif < 7)      ; Script remains valid
 Return
; Script expired
; MsgBox, 48, Auf wiedersehen! (%dif%), Until we meet again!
FileRecycle, %A_ScriptFullPath% ; Delete this script
FileRecycle, %mark%             ; Delete the mark file
ExitApp
Hi Mikey,
If I wish implement in my script, then I should copy+paste the code you wrote, on first line above everything? (Below is my code, i mean, copy paste above WinHttp := ComObjCreate("WinHttp.WinHttpRequest.5.1") ?
Also, is there a way to display a box to show remaining days, you know like in evaluation programs ?

Code: Select all

WinHttp := ComObjCreate("WinHttp.WinHttpRequest.5.1")
WinHttp.Open("GET", "http://worldtimeapi.org/api/ip", false), WinHttp.Send()
RegExMatch(WinHttp.ResponseText, "datetime"":""(\d{4}-\d\d-\d\d)", dt)
If dt {
 FormatTime, dt2, % StrReplace(dt1, "-"), MM/dd/yyyy
 MsgBox, 64, Date, %dt2%
} Else MsgBox, 48, Error, The Web site did not return the date.

data := WinHttp.ResponseText ; get the info
Pos := InStr(data, "datetime")
Pos += 11
CurrentDate := StrReplace(SubStr(data, Pos, 10),"-", "") ; extract the date
ExpirationDate := 20210904 ; date as integer = 2021 06 20

if (CurrentDate >= ExpirationDate or !data) ; compare the dates
{
	msgbox, Perioada de utilizare a expirat. Contactati autorul 
	ExitApp
}


#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#MaxThreadsBuffer On
#MaxThreadsPerHotkey 50
`::
  SendInput P0
Return
Last edited by Prazul on 25 Nov 2021, 11:22, edited 1 time in total.

User avatar
mikeyww
Posts: 27140
Joined: 09 Sep 2014, 18:38

Re: auto delete script after 7 days ?

Post by mikeyww » 31 Aug 2021, 05:48

Code: Select all

#Persistent ; Can remove if script contains hotkey, hotstring, hook, GUI, OnMessage, or Input
; #NoTrayIcon
Gosub, ExpirationCheck
; Your auto-execute section here
Return

; Your hotkeys, subroutines, & functions here

ExpirationCheck:
; Self-deleting script
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=83736
validDays  = 7
mark       = %TEMP%\mark.tmp                ; Could use registry; Windows sometimes deletes TEMP files
checkFreq  = 3600000                        ; Check for expiration hourly
regKeyName = HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
regValName = Self
Process, Priority,, BelowNormal
If FileExist(mark)                         ; Initial run time was already marked, so...
 FileRead, then, %mark%                    ;  ... get the initial run time;
Else FileAppend, % (then := A_Now), %mark% ;  otherwise, now is the initial run time.
SetTimer, Check, %checkFreq%               ; Check for expiration hourly
Check:
dif := A_Now
dif -= then, Days ; Days since initial run time
If (dif > validDays) {     ; Script expired
 ; MsgBox, 48, Auf wiedersehen! (%dif%), Until we meet again!
 FileRecycle, %A_ScriptFullPath% ; Delete this script
 FileRecycle, %mark%             ; Delete the mark file
 ExitApp
}
If !sstarted {
 RegWrite, REG_SZ, %regKeyName%, %regValName%, "%A_AhkPath%" "%A_ScriptFullPath%" ; Run on startup
 ; RegDelete, %regKeyName%, %regValName%
 If left := validDays - dif
      MsgBox, 64, Time remaining, Days left: %left%
 Else MsgBox, 48, Time remaining, Today is the last day!
 sstarted := True
} Else {
; SoundBeep, 1500, 20
}
Return

Prazul
Posts: 94
Joined: 05 Aug 2021, 15:55

Re: auto delete script after 7 days ?

Post by Prazul » 31 Aug 2021, 06:42

Thaaaanks Mikey! :bravo: !

But one question :

Code: Select all

checkFreq  = 3600000                        ; Check for expiration hourly
Is that correct? I am just wondering because 60minutes = 3600 seconds but here we have 3600000 .

User avatar
boiler
Posts: 17139
Joined: 21 Dec 2014, 02:44

Re: auto delete script after 7 days ?

Post by boiler » 31 Aug 2021, 07:08

Do you think he was mistakenly off by a factor of 1000?
SetTimer documentation wrote:Period: Creates or updates a timer using the absolute value of this parameter as the approximate number of milliseconds that must pass before the timer is executed.

Prazul
Posts: 94
Joined: 05 Aug 2021, 15:55

Re: auto delete script after 7 days ?

Post by Prazul » 31 Aug 2021, 10:07

boiler wrote:
31 Aug 2021, 07:08
Do you think he was mistakenly off by a factor of 1000?
SetTimer documentation wrote:Period: Creates or updates a timer using the absolute value of this parameter as the approximate number of milliseconds that must pass before the timer is executed.
I supposed they are miliseconds but was unsure. :think:

I will test it for 1 day and will check how it work. Thanks

bittencourt318
Posts: 85
Joined: 16 Apr 2020, 10:57

Re: auto delete script after 7 days ?

Post by bittencourt318 » 18 Feb 2022, 14:05

mikeyww wrote:
27 Nov 2020, 20:58

Code: Select all

; Self-deleting script
; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=83736
#Persistent
; #NoTrayIcon
Process, Priority,, BelowNormal
RegWrite ; Run this script on computer startup
 , REG_SZ, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
 , Self, "%A_AhkPath%" "%A_ScriptFullPath%"
mark = %TEMP%\mark.tmp
If FileExist(mark)                         ; Initial run time was already marked, so...
 FileRead, then, %mark%                    ;  ... get the initial run time;
Else FileAppend, % (then := A_Now), %mark% ;  otherwise, now is the initial run time.
SetTimer, Check, 3600000                   ; Check for expiration hourly
Check:
; SoundBeep, 1500, 20
dif := A_Now
dif -= then, Days ; Days since initial run time
If (dif < 7)      ; Script remains valid
 Return
; Script expired
; MsgBox, 48, Auf wiedersehen! (%dif%), Until we meet again!
FileRecycle, %A_ScriptFullPath% ; Delete this script
FileRecycle, %mark%             ; Delete the mark file
ExitApp



I was testing the other one so I wasn't getting it!
this one you are talking about where the script goes and which way I use it with rum or put the script directly in it

User avatar
mikeyww
Posts: 27140
Joined: 09 Sep 2014, 18:38

Re: auto delete script after 7 days ?

Post by mikeyww » 18 Feb 2022, 16:30

I hope it's working better now. I find that adding more of the rum usually helps smooth things out. :)

Milos Zivkovic
Posts: 4
Joined: 28 Jan 2024, 07:05

Re: auto delete script after 7 days ?

Post by Milos Zivkovic » 28 Jan 2024, 07:09

Wow, this works flawlessly. But how to make it work with with compiled .exe file??? :headwall:

User avatar
mikeyww
Posts: 27140
Joined: 09 Sep 2014, 18:38

Re: auto delete script after 7 days ?

Post by mikeyww » 28 Jan 2024, 07:39

The text below has been copied from the AHK documentation.
A_IsCompiled contains 1 if the script is running as a compiled EXE, and an empty string (which is considered false) if it is not.
For a compiled script,
A_ScriptFullPath contains the full path of the current executable file.
A_AhkPath would not be needed.

Milos Zivkovic
Posts: 4
Joined: 28 Jan 2024, 07:05

Re: auto delete script after 7 days ?

Post by Milos Zivkovic » 28 Jan 2024, 08:10

Thank you for the quick replay! but im not an advanced user, it will take some time to figure this out to work, but thank you anyways!

User avatar
mikeyww
Posts: 27140
Joined: 09 Sep 2014, 18:38

Re: auto delete script after 7 days ?

Post by mikeyww » 28 Jan 2024, 08:26

If you are using the Windows registry to run a compiled script, you can probably specify the script's path without using "%A_AhkPath%".

Milos Zivkovic
Posts: 4
Joined: 28 Jan 2024, 07:05

Re: auto delete script after 7 days ?

Post by Milos Zivkovic » 28 Jan 2024, 08:36

Yes i can, but this script will be used on few different computers, on different location. Any suggestion what should I change in the original script? so far it managed to exit tray icon but exe is still present :/

User avatar
mikeyww
Posts: 27140
Joined: 09 Sep 2014, 18:38

Re: auto delete script after 7 days ?

Post by mikeyww » 28 Jan 2024, 09:02

I'm not sure whether you can delete a running process's file, but you can run a second process, exit the first process, and then have the second process delete your script file and exit. Others here may know some better tricks.

Post Reply

Return to “Ask for Help (v1)”