Announcement

Collapse
No announcement yet.

How do I Monitor File's Content For Changes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How do I Monitor File's Content For Changes

    That may not even be the correct question, but here's what I'm trying to do.
    Another application is writing a device number into a file.

    I have a program that reads the file. The information is used to send a turn-on command to a device. The device automatically turns off after a second, so while the first program continues, every two seconds, the second program has to send the turn-on command to the device until the number in the file changes to another number or it becomes nothing.

    Back to the question, what is the best way to monitor the file for change?
    I hope this makes sense.
    Thanks,
    Jamie
    Jamie

    #2
    I've read that monitoring a directory can be unreliable.
    I've monitored a file for many years with a timer and just
    compare the current buffer with a GET. There are ways to let
    the operating system report it to you, but from reading be sure
    that you use a technique that updates the file so the operating
    system always catches the change.

    Comment


      #3
      Shouldn't this line of reasoning work?

      Code:
      STATIC oldFileTime AS FILETIME ' use this to hold last known write time
      LOCAL myFileTime AS FILETIME
      
        IF Exist(<filespec>, fileInfo) THEN
      ' -- Convert file time from UTC to local time
          FileTimeToLocalFileTime fileInfo.ftLastWriteTime, myFileTime
      ' Compare the LastWrittenFileTimes
          retVal  = CompareFileTime(myFileTime, oldFileTime)
          SELECT CASE retVal
            CASE 0
      '     ~~~~~~
      ' Equal, do nothing
            CASE 1
      '     ~~~~~~
      ' Newer filetime - process
           END SELECT
        END IF
      
      '===========
      FUNCTION Exist(BYVAL FileSpec AS STRING, fileInfo AS WIN32_FIND_DATA) AS LONG
      ' - Return true (-1) if filespec exists.
        LOCAL hFind AS DWORD
      
        IF LEN(FileSpec) THEN
          hFind = FindFirstFile(BYCOPY FileSpec, fileInfo)
          IF hFind <> %INVALID_HANDLE_VALUE THEN
            FindClose hFind
            FUNCTION = %TRUE
          ELSE
            FUNCTION = %FALSE
          END IF
        ELSE
          FUNCTION = %FALSE
        END IF
      
      END FUNCTION
      :) IRC :)

      Comment


        #4
        Win32(SDK): Internet Cookie Monitor April 25, 2001

        Works for any folder, not just the cookie folder.

        >I've read that monitoring a directory can be unreliable.
        You must be spending too much time on the Internet.
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


          #5
          The cookie monitor code needs to be rewritten for current compilers.
          It might contain code he could use.

          Comment


            #6
            Do all solutions involve a sleep or wait timer loop to keep checking the file, irregardless of whether it's changed or not?
            Jamie

            Comment


              #7
              No.
              FindFirstChangeNotification
              WaitForMultipleObjects

              Comment


                #8
                This was discussed before.

                User to user discussions about the PB/Win (formerly PB/DLL) product line. Discussion topics include PowerBASIC Forms, PowerGEN and PowerTree for Windows.

                Comment


                  #9
                  Code:
                  'If you hear a BEEP a change occurred
                  #DIM ALL
                  #INCLUDE "win32api.inc"
                  GLOBAL gWaitInMilliseconds AS LONG
                  GLOBAL gDirectoryToWatch AS ASCIIZ * 64
                  FUNCTION PBMAIN AS LONG
                    LOCAL hThread???, var&, h&, x&, lResult&
                    gWaitInMilliseconds = 3000
                    gDirectoryToWatch  = "\keep\email"
                    MKDIR gDirectoryToWatch   'create directory
                    ERRCLEAR
                    THREAD CREATE WatchDirectory(var&) TO hThread???
                    SLEEP 50
                    THREAD CLOSE hThread TO lResult
                    h = FREEFILE
                    OPEN gDirectoryToWatch  + "\TEST" FOR APPEND AS #h
                    FOR x = 1 TO 5                                         'write something every second
                      PRINT  #h, "MORE"
                      IF ERR THEN ? STR$(ERR)
                      SLEEP 3000
                    NEXT
                    CLOSE #h
                    gWaitInMilliseconds = 0                                'signal thread to end so program ends
                    SLEEP 4000  'give thread time to end
                  END FUNCTION
                  FUNCTION WatchDirectory (BYVAL x AS LONG) AS LONG
                    LOCAL lDir AS DWORD, found AS DWORD
                    DO
                      lDir = FindFirstChangeNotification(gDirectoryToWatch, 0, %FILE_NOTIFY_CHANGE_SIZE)
                      Found = WaitForSingleObject(lDir, gWaitInMilliseconds)
                      IF found = 0 THEN
                        'directory changed
                        BEEP                      'change occurred
                      END IF
                      FindCloseChangeNotification(lDir)
                    LOOP UNTIL gWaitInMilliseconds=0
                    ? "Thread done"
                  END FUNCTION

                  Comment


                    #10
                    >The cookie monitor code needs to be rewritten for current compilers
                    A.
                    Code:
                    'Program to trap and diplay changes to Internet cookies
                    '   Author: Michael Mattias, Racine WI April, 2001  using PB/DLL v 6.0
                    '   Idea from Greg D. Engle
                    '   Cookie Icons courtesy Dan Stasinski and Bob Houle
                    '   [B]Placed in the public domain by the author April 23, 2001[/B]

                    B. This whine's time has come
                    Michael Mattias
                    Tal Systems (retired)
                    Port Washington WI USA
                    [email protected]
                    http://www.talsystems.com

                    Comment


                      #11
                      I believe that gets me what I'm looking for.
                      Thanks
                      Jamie

                      Comment


                        #12
                        Do all solutions involve a sleep or wait timer loop to keep checking the file, irregardless of whether it's changed or not?
                        More generally, Windows provides far far more efficient methods of waiting than a loop which checks a condition each time thru.

                        If you are waiting for 'something to happen' there are specific system objects used by the WaitForSingleObject and/or WaitFor multipleObjects and/or MsgWaitforMultipleObjects functions; see those functions in your WInApi reference to locate all the objects supported. And, you can always assign your own "event" on which to wait, as demo'd here:
                        Terminate Worker Threads Using Windows Events (and Using Thread Local Storage) Demo Dec 23 2005

                        To wait for a 'time certain' rather than some 'event', here's a demo of using the system object designed for that purpose:
                        Waitable Timer Object Demo June 2005

                        MCM
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment

                        Working...
                        X
                        😀
                        🥰
                        🤢
                        😎
                        😡
                        👍
                        👎