Page 1 of 1

Here is a Folder Change monitor (win)

Posted: Thu Sep 16, 2004 8:03 am
by Dare2
The following monitors a folder and advises on changes.

Questions of a technical nature will be ignored due to ignorance on my part. :P

Comments, suggestions, improvements and (if this is not good or safe) warnings are requested.

Hope it is ok, and hope it is useful.

Code: Select all

; Monitor changes to a folder, by Dare2
; First test
; Windows 2K and PB3.91
;
; Change "pth" var below to hold a folder name
; Do something to the folder, eg,
; Copy in a file then delete it.
;
; If your windows are crowded you might
; have to hunt for the messagebox!  :)


#STATUS_WAIT_0=$00000000            ; Taken from windows inc
#STATUS_ABANDONED_WAIT_0=$00000080  ; Not all are used here.
#STATUS_USER_APC=$000000C0
#STATUS_TIMEOUT=$00000102

#WAIT_FAILED=-1
#WAIT_OBJECT_0=#STATUS_WAIT_0
#WAIT_ABANDONED=#STATUS_ABANDONED_WAIT_0
#WAIT_ABANDONED_0=#STATUS_ABANDONED_WAIT_0
#WAIT_TIMEOUT=#STATUS_TIMEOUT
#WAIT_IO_COMPLETION=#STATUS_USER_APC

fHnd.l

;                                          Decide which notifications
flgs=0
flgs=flgs | #FILE_NOTIFY_CHANGE_FILE_NAME
flgs=flgs | #FILE_NOTIFY_CHANGE_FILE_NAME
flgs=flgs | #FILE_NOTIFY_CHANGE_DIR_NAME
flgs=flgs | #FILE_NOTIFY_CHANGE_ATTRIBUTES
flgs=flgs | #FILE_NOTIFY_CHANGE_SIZE
flgs=flgs | #FILE_NOTIFY_CHANGE_LAST_WRITE
flgs=flgs | #FILE_NOTIFY_CHANGE_SECURITY

pth.s="C:\aFolder\path\required\"
fHnd=FindFirstChangeNotification_(pth,#False,flgs)
If fHnd=#INVALID_HANDLE_VALUE
  MessageRequester("ERROR","FirstChange returned "+Str(GetLastError_()),#MB_ICONSTOP)
  End
EndIf

;                            Only watches for 1 minute or 2 events, which is sooner.
timeout=60000
tm=GetTickCount_()+timeout
While ctr<2 And tm>GetTickCount_()
  res.l=FindNextChangeNotification_(fHnd)  ; Advise we want a notification
  If res=#False
    MessageRequester("ERROR","NextChange returned "+Str(GetLastError_()),#MB_ICONSTOP)
    ctr=3
  Else
;                           Wait for notification
    chng=WaitForSingleObject_(fHnd,timeout)   ;#INFINITE)
    ctr+1
    If chng=#WAIT_OBJECT_0
      MessageRequester("NOTICE!","Change Notification!",#MB_ICONEXCLAMATION)
    Else
      MessageRequester("NOTICE!","ANOTHER EVENT!",#MB_ICONSTOP)
    EndIf
    res.l=FindNextChangeNotification_(fHnd)
    If res=#False
      MessageRequester("ERROR","NextChange returned "+Str(GetLastError_()),#MB_ICONSTOP)
      ctr=3
    EndIf
  EndIf
Wend
res=FindCloseChangeNotification_(fHnd)
Still have to try monitoring multiple folders via:
WaitForMultipleObjects_(count,AddressOfArrayOfHandles,#False,timeout)

Posted: Thu Sep 16, 2004 11:08 am
by dige
Thanks for sharing! It's very useful for my current project :) thx.

cya dige

Posted: Fri Sep 17, 2004 11:50 am
by Wolf
Thanks dare2 it's great work :wink:

Posted: Fri Sep 17, 2004 2:41 pm
by Dare2
Thanks. :)

Still to do the multiple objects.

Also stuck on a way to monitor changes to registry in general (can only see approaches that monitor a specific key).

Posted: Fri Sep 17, 2004 6:15 pm
by blueznl
my memroy is failing me (as usual :-)) but there was some api that handled / flagged / monitored directories, and it wasn't the one you used... if i find the sample back i'll post it

Posted: Sat Sep 18, 2004 12:02 am
by Dare2
Appreciate that. :)

The single item wait I'm using is, I read, a begger on the queues and can degrade performance unless short time outs, etc.