[REQ] Changing Custom CheckFileTime Script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
thebib622
Posts: 25
Joined: 01 Aug 2017, 17:09

[REQ] Changing Custom CheckFileTime Script

31 Aug 2023, 18:56

I have a custom CheckFileTime script that checks if a specific file has been changed. This check occurs every second. Once a change is detected, it will send the forward-slash "\" command.
However, I would like to have the script behave differently now.

I want the script to detect a certain amount of time has passed without any changes made to a specific file. Once that amount of time has passed, then I would like the forward-slash "\" command to be sent.

My goal with this revision request is to cutdown on the number of "\" commands being sent.

For example, I want the script to send the "\" command if my specific txt file has not changed in 2 minutes.
If a change has been detected in 1:59 or less, then the script simply does nothing, and will continue to wait until the next update to the file occurs.

The script I have now is listed below.

Code: Select all

;=================================
;these 4 items are the only ones you need to modify to make this
; routine work. They should already be set up properly.
;------------------------------------------------------------
; this is the file path and file name of the file you want to check
; the timestamps on
filename = C:\file.txt

; this variable will let you check every second to see if the  file
; has been modified. I don't recommend you set it lower than 500.
; go higher than 1000 if your game starts to lag from all the file  checks.
sleeptime = 1000

; this is the name that appears on the top border of your game window
windowname = Team Fortress 2

; this is the background color of the script gui in RGB format (teal blue)
gui, color, 0x20f0f0

;=================================
;----------------------------------------------------------
; DO NOT MODIFY ANY CODE FROM THIS POINT ON
;----------------------------------------------------------
SetTitleMatchMode, 2
;This is a gui that will let you stop the loop when you're done
Gui, +Resize
Gui, Add, Text,, CheckFileTime
Gui, Add, Button, +E0x0101 w60, Quit
Gui, Show,    , CheckFileTime

firstcall = 0
Gosub, CheckFileTime

loop
{
    Gosub, CheckFileTime
    Sleep, %sleeptime%
}

ButtonQuit:
GuiClose:
ExitApp

;---------------------------------------------------------
; this routine will automatically send the slash to the tf2 window
; when a diff in the timestamps is detected. It will also auto reset
; the marker time for the next comparison once the compare indicates
; that the file was changed.
;--------------------------------------------------------
; DO NOT MODIFY ANYTHING IN THIS ROUTINE
;--------------------------------------------------------
CheckFileTime:
; on a first call to this routine (when firstcall = 0) this routine will
; set up the initial marker timestamp
IfEqual, firstcall, 0
{   
    FileGetTime, FirstTimeStamp, %filename%, M
    FirstCall = 1
    return
}

; At this point the initial timestamp is available so we get the time
; stamp again to see if it's different from the marker
FileGetTime, CurrTimeStamp, %filename%, M

; if it's different than the marker...
IfNotEqual, CurrTimeStamp, %FirstTimeStamp%
{   
    ;activate the tf2 window and send the \ key
    WinActivate, %windowname%
    Sleep, 100
    Send, \
    Sleep, 100
    ; since we have a difference and have sent the \ key we set up
    ; a new marker time
    FirstCall = 0
    Goto, CheckFileTime
}
Return
[Mod edit: replaced quote tags with [code][/code] tags.]
[Mod edit: Moved topic from AHK v2 help to v1 help, since this is v1 code.]
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: [REQ] Changing Custom CheckFileTime Script

31 Aug 2023, 19:24

Hello,

1. When a change is detected, set a timer.
2. When the timer expires, send your special key.
User avatar
DataLife
Posts: 460
Joined: 29 Sep 2013, 19:52

Re: [REQ] Changing Custom CheckFileTime Script

31 Aug 2023, 21:42

mikeyww wrote:
31 Aug 2023, 19:24
Hello,

1. When a change is detected, set a timer.
2. When the timer expires, send your special key.
What if the file changed again while the timer was running? Wouldn't you have to keep checking the timestamp and reset the timer if the timestamp changed?
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: [REQ] Changing Custom CheckFileTime Script

31 Aug 2023, 21:46

No, because if you detect a change, you will set the timer. If it is already running, it will be reset automatically.
User avatar
Smile_
Posts: 859
Joined: 03 May 2020, 00:51

Re: [REQ] Changing Custom CheckFileTime Script

31 Aug 2023, 21:52

Maybe something like the following

Code: Select all

FileName := "C:\file.txt"
WindowName := "Team Fortress 2"
TimerInterval := 1000
Counter := 0
WaitFor := 120 ; 2 minutes

FileGetTime, LastTimeStamp, % FileName, M
Gui, Add, Text, w300 Center, CheckFileTime
Gui, Add, Button, wp, Quit
Gui, Show,, CheckFileTime
SetTimer, CheckFileTime, % TimerInterval
Return

CheckFileTime:
    FileGetTime, TimeStamp, % FileName, M
    If (TimeStamp != LastTimeStamp) {
        LastTimeStamp := TimeStamp
        Counter := 0
    } Else If (++Counter = WaitFor) {
        WinActivate, % WindowName
        Sleep, 100
        Send, \
        Sleep, 100
    } Else If (Counter > WaitFor + 1) {
        Counter := WaitFor + 1
    }
Return

ButtonQuit:
GuiClose:
ExitApp
Last edited by Smile_ on 31 Aug 2023, 22:11, edited 1 time in total.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: [REQ] Changing Custom CheckFileTime Script

31 Aug 2023, 21:58

Mine was similar, without GUI.

Code: Select all

#Requires AutoHotkey v1.1.33
#Persistent
filePath := A_ScriptDir "\test2.txt"
wait     := 4000
Process Priority,, B
SetTimer Check, 500
Check:
last := time
FileGetTime time, % filePath
If (last != "" && time > last) {
 SetTimer Done, % -wait
 SoundBeep 2500
}
Return

Done:
Send \
SoundBeep 1500
Return
RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: [REQ] Changing Custom CheckFileTime Script

01 Sep 2023, 06:31

thebib622 wrote: ... I would like the forward-slash "\" command to be sent.
Just as an FYI, the \ character is the backslash, not the forward slash - Google it - you'll see I'm right. I mention this only because if you are trying to offer someone technical support of some type over the phone and you keep telling them to "type the forward slash" when you mean \, you will never understand why what you're telling them doesn't work. :)

Russ

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], macromint, peter_ahk and 341 guests