Here is a Folder Change monitor (win)

Everything else that doesn't fall into one of the other PB categories.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Here is a Folder Change monitor (win)

Post 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)
@}--`--,-- A rose by any other name ..
dige
Addict
Addict
Posts: 1391
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

Thanks for sharing! It's very useful for my current project :) thx.

cya dige
Wolf
Enthusiast
Enthusiast
Posts: 232
Joined: Sat Apr 03, 2004 12:00 pm
Location: S.T

Post by Wolf »

Thanks dare2 it's great work :wink:
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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).
@}--`--,-- A rose by any other name ..
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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.
@}--`--,-- A rose by any other name ..
Post Reply