Help with using Tail/Checkfile to watch multiple files

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
robs1990

Help with using Tail/Checkfile to watch multiple files

29 Nov 2014, 04:21

Hello!

I've been trying to use this function that I found, the purpose of it is to monitor a file's contents. When I run it monitoring just one file, I test it by modifying the text file manually and it succedes in showing me what I have updated the file with. However when I try it with two files, I don't get any updates in the message boxes. Does anybody know if it is possible to use this function to monitor multiple files? I'm a bit stuck! Thanks!

Code: Select all

#Persistent
file1=C:\Users\Rob\Desktop\f1.txt
file2=C:\Users\Rob\Desktop\f2.txt

SetTimer, FILE_CHECK, 1000


FILE_CHECK:
if (x1 := checkfile(file1)) {
msgbox x1=%x1%
}
if (x2 := checkfile(file2)) {
msgbox x2=%x2%
}
return


CheckFile(File) {
   ; THX Sean for File.ahk : http://www.autohotkey.com/forum/post-124759.html
   Static CF := ""   ; Current File
   Static FP :=     ; File Pointer
   Static OPEN_EXISTING := 3
   Static GENERIC_READ := 0x80000000
   Static FILE_SHARE_READ := 1
   Static FILE_SHARE_WRITE := 2
   Static FILE_SHARE_DELETE := 4
   Static FILE_BEGIN := 0
   BatchLines := A_BatchLines
   SetBatchLines, -1
   If (File != CF) {
      CF := File
      FP := 0
   }
   hFile := DllCall("CreateFile"
                  , "Str",  File
                  , "Uint", GENERIC_READ
                  , "Uint", FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE
                  , "Uint", 0
                  , "Uint", OPEN_EXISTING
                  , "Uint", 0
                  , "Uint", 0)
   If (!hFile) {
      CF := ""
      FP := 0
      SetBatchLines, %BatchLines%
      Return False
   }
   DllCall("GetFileSizeEx"
         , "Uint",   hFile
         , "Int64P", nSize)
   If (FP = 0 Or nSize <= FP) {
      FP := nSize
      SetBatchLines, %BatchLines%
      DllCall("CloseHandle", "Uint", hFile) ; close file
 Return False
   }
   DllCall("SetFilePointerEx"
         , "Uint",  hFile
         , "Int64", FP
         , "Uint",  0
         , "Uint",  FILE_BEGIN)
   VarSetCapacity(Tail, Length := nSize - FP, 0)
   DllCall("ReadFile"
         , "Uint",  hFile
         , "Str",   Tail
         , "Uint",  Length
         , "UintP", Length
         , "Uint",  0)
   DllCall("CloseHandle", "Uint", hFile)
   VarSetCapacity(Tail, -1)
   FP := nSize
   SetBatchLines, %BatchLines%
   Return Tail
} file=Script.ahk]
hd0202
Posts: 183
Joined: 04 Oct 2013, 03:07
Location: Germany near Cologne

Re: Help with using Tail/Checkfile to watch multiple files

29 Nov 2014, 08:02

In your current version you call use it for only one file. The cause are the static variables "CF" and FP" which keep the values from one call to the next, and if you call it with another file they are reset.

Here is my suggestion that uses an array for this purpose and can be used for many files:
Spoiler
Hubert

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: dipahk, Nerafius, RandomBoy and 177 guests