Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have Autohotkey 'listen' for a change to a file?

Tags:

autohotkey

I have a text file, lets call it C:\to_run.txt. I'd like my Autohotkey script to 'listen' to this file in such a way that when it detects a change, it performs an action immediately based on the contents of the file, and then make the file blank again.

I can handle the last parts, so really I'm asking for an efficient way to detect file changes? I say efficient because my Autohotkey script is getting rather long and I don't want this listening function to hang up the rest of the script in any way.

like image 902
brollin Avatar asked Dec 20 '22 03:12

brollin


1 Answers

Assuming we are really talking of only one file to check on: Surely not as beautiful as Sidola's answer, but without the need for external libraries:

#persistent

lastFileContent := ""
setTimer, checkFile, 20
return

checkFile:
fileread newFileContent, changingDocument.txt
if(newFileContent != lastFileContent) {
    lastFileContent := newFileContent
    msgbox, content changed to: %newFileContent%
}
return

In this case, for checking on larger files, it might be better to compare MD5-checksums instead of the whole file content.

Note: I have not tested the performance implications on this. This script opens up the file 50 times per second, could be pretty hard drive consuming.

like image 96
phil294 Avatar answered Mar 02 '23 18:03

phil294