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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With