WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

How To
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

File System Notification on Change

Keywords: 	  File System Notification on Change File Change Notification

Here's how you can use the windows "File Change Notification". The examples below only watch a single directory, but I think it would work with more than one.

The change notification arrives when anything in the directory changes - a file could be deleted, modified, or created. Once the notification arrives you'll still have to search the directory to see just what changed.


;
;
;  Simple example...  Register and wait
;
WatchDir = "C:\Windows\Desktop\Incoming"
Kernel32 = DllLoad(strcat(dirwindows(1),"KERNEL32.DLL"))
WatchHandle = DLLCALL(Kernel32,long:"FindFirstChangeNotificationA",lpstr:WatchDir,long:1,long:1)
Status = DLLCALL(kernel32,long:"WaitForSingleObject",long:WatchHandle,long:-1)
DLLCALL(Kernel32,long:"FindCloseChangeNotification",long:WatchHandle)
DllFree(Kernel32)
Message ("Hello!","Something Changed!")


;
;  More complex example.
;  Register, then go into a loop processing changes, or waiting for
;  a button to be pushed.  (We actually wait with a 1 second timeout.
;  At timeout we test the button.  If it wasn't pushed we wait another
;  second.)
;
WatchDir = "C:\Windows\Desktop\Incoming"
Kernel32 = DllLoad(strcat(dirwindows(1),"KERNEL32.DLL"))
WatchHandle = DLLCALL(Kernel32,long:"FindFirstChangeNotificationA",lpstr:WatchDir,long:1,long:1)

BoxesUp("25,25,550,200",@NORMAL)
BoxCaption(1,"Waiting")
BoxDrawText(1,"50,50,950,200",Strcat("Waiting for files in: ",WatchDir),@TRUE,0)
BoxButtonDraw(1,1,"Cancel","300,500,550,900")

while (1)
    WaitStatus = DLLCALL(kernel32,long:"WaitForSingleObject",long:WatchHandle,long:1000)
    if BoxButtonStat(1,1) 
         Message ("Cancelled.","Wait Process Cancelled by user.")
         break
    endif
    if WaitStatus == 0
         Message (WatchDir,"Something Changed!")
    endif
    if BoxButtonStat(1,1) then break
    DLLCALL(Kernel32,long:"FindNextChangeNotification",long:WatchHandle)
endwhile

DLLCALL(Kernel32,long:"FindCloseChangeNotification",long:WatchHandle)
DllFree(Kernel32)
return

Article ID:   W15512
File Created: 2003:05:13:11:28:26
Last Updated: 2003:05:13:11:28:26