auto delete script after 7 days ? Topic is solved
auto delete script after 7 days ?
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 ?
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 ?
Re: auto delete script after 7 days ? Topic is solved
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
Re: auto delete script after 7 days ?
Nice solution. Congratulations.mikeyww wrote: ↑27 Nov 2020, 20:58Code: 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
Re: auto delete script after 7 days ?
yea he is soo good
Re: auto delete script after 7 days ?
Thank you!
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.
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.
Re: auto delete script after 7 days ?
My script name is : SCRIPT1
Whats should i change in this code to make it work correctly and automatic delete after 7 days?
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
Re: auto delete script after 7 days ?
You would move your earlier auto-execute section to the top, since these lines will no longer execute in their current position.
I'm not seeing any reason for #Persistent in your script.
Code: Select all
RClick := 1
LClick := 1
QSCOPE := 1
Re: auto delete script after 7 days ?
Hi Mikey,mikeyww wrote: ↑27 Nov 2020, 20:58Code: 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
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.
Re: auto delete script after 7 days ?
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
Re: auto delete script after 7 days ?
Thaaaanks Mikey! !
But one question :
Is that correct? I am just wondering because 60minutes = 3600 seconds but here we have 3600000 .
But one question :
Code: Select all
checkFreq = 3600000 ; Check for expiration hourly
Re: auto delete script after 7 days ?
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.
Re: auto delete script after 7 days ?
I supposed they are miliseconds but was unsure.boiler wrote: ↑31 Aug 2021, 07:08Do 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 will test it for 1 day and will check how it work. Thanks
-
- Posts: 85
- Joined: 16 Apr 2020, 10:57
Re: auto delete script after 7 days ?
mikeyww wrote: ↑27 Nov 2020, 20:58Code: 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
Re: auto delete script after 7 days ?
I hope it's working better now. I find that adding more of the rum usually helps smooth things out.
-
- Posts: 4
- Joined: 28 Jan 2024, 07:05
Re: auto delete script after 7 days ?
Wow, this works flawlessly. But how to make it work with with compiled .exe file???
Re: auto delete script after 7 days ?
The text below has been copied from the AHK documentation.
For a compiled script,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.
A_AhkPath would not be needed.A_ScriptFullPath contains the full path of the current executable file.
-
- Posts: 4
- Joined: 28 Jan 2024, 07:05
Re: auto delete script after 7 days ?
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!
Re: auto delete script after 7 days ?
If you are using the Windows registry to run a compiled script, you can probably specify the script's path without using "%A_AhkPath%".
-
- Posts: 4
- Joined: 28 Jan 2024, 07:05
Re: auto delete script after 7 days ?
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 :/
Re: auto delete script after 7 days ?
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.