is there a way using declares etc… to receive a signal if a folder has been modified?
I.e. a file added or deleted or updated.
To be used as a file sync program.
is there a way using declares etc… to receive a signal if a folder has been modified?
I.e. a file added or deleted or updated.
To be used as a file sync program.
Many thanks Andrew,
For any others who come here you need 2 methods and 1 property
Property
mChangeHandles as dictionary
method 1 - which you call to add the folder to the watch list, it can also watch sub-directorys
create a method called StartWatching(f as folderitem, WatchSubDirs as boolen)
[code] Soft Declare Function FindFirstChangeNotificationA Lib “Kernel32” ( path as CString, watchSubDirs as Boolean, flags as Integer ) as Integer
Soft Declare Function FindFirstChangeNotificationW Lib “Kernel32” ( path as WString, watchSubDirs as Boolean, flags as Integer ) as Integer
Const FILE_NOTIFY_CHANGE_FILE_NAME = &h00000001
Const FILE_NOTIFY_CHANGE_DIR_NAME = &h00000002
// Try to start watching for changes
dim handle as Integer
if System.IsFunctionAvailable( “FindFirstChangeNotificationW”, “Kernel32” ) then
handle = FindFirstChangeNotificationA( f.AbsolutePath, watchSubDirs, FILE_NOTIFY_CHANGE_FILE_NAME + FILE_NOTIFY_CHANGE_DIR_NAME )
else
handle = FindFirstChangeNotificationW( f.AbsolutePath, watchSubDirs, FILE_NOTIFY_CHANGE_FILE_NAME + FILE_NOTIFY_CHANGE_DIR_NAME )
end if
if handle <> -1 then
// If we don’t have a handle map, then make one
if mChangeHandles = nil then mChangeHandles = new Dictionary
// And store this handle since we'll need it
mChangeHandles.Value( f.AbsolutePath ) = handle
end
[/code]
second method is CheckFolder(f as folderitem) as Boolean
[code] // Check to see if this folder item has a handle in the map
dim handle as Integer
try
handle = mCHangeHandles.Value( f.AbsolutePath )
catch
// If this didn’t work, then something’s wrong! Either there’s
// no map, or the folder item isn’t in it. Either way, bail out
return false
end
// Now that we have a handle, let’s try to see if anything’s changed
// with it
Declare Function WaitForSingleObject Lib “Kernel32” ( handle as Integer, waitTime as Integer ) as Integer
dim status as Integer
status = WaitForSingleObject( handle, 0 )
// If we’ve had a change, the status will be WAIT_OBJECT_0
Const WAIT_OBJECT_0 = &h0
if status = WAIT_OBJECT_0 then
// We’ve had a change! Reset the handle so we can use it again
Declare Sub FindNextChangeNotification Lib “Kernel32” ( handle as Integer )
FindNextChangeNotification( handle )
// And return the change
return true
end
// If we got here, no change has taken place!
return false
[/code]
first you call startwatching(f, true) then set a timer to check if the folder has changed
if checkFolder(f) = true then
’ has been changed
else
’ not changed
end if
Just glanced it over in passing, you check for FindFirstChangeNotificationW then use FindFirstChangeNotificationA
I’d only use the FindFirstChangeNotificationW version too.