Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:19 pm
by Jade Support >> Tue, 3 Jun 2008 21:39:07 GMT
I recently wrote an application that does just this. It uses a combination of the Windows API and a JADE timer to detect file changes in n number of monitored directories. The legwork of having to iterate all folders and files checking for changes is removed because Windows will tell us when and where a change has occurred. The entire project is contained within just a few lines of code, below...
Define the following two external functions in kernel32:
findFirstChangeNotification(lpPathName : String; bWatchSubtree, dwNotifyFilter : Integer) : Integer is FindFirstChangeNotificationA in kernel32;
waitForMultipleObjects(nCount, pHandles, fWaitAll, dwMilliseconds : Integer) : Integer is WaitForMultipleObjects in kernel32;
The code should be easy enough to follow (the ListBox, lbxWatchedFolder, is populated on startup):
btnStartStop_click(btn: Button input) updating;
constants
FILE_NOTIFY_CHANGE_FILE_NAME : Integer = #1;
vars
dirHandle : Integer;
i, iOffset : Integer;begin
if btnStartStop.caption = "Start Spying..." then
// Variable initialisation
handlesArray := null;
iOffset := 1;
i := 1;
// Build watch list
foreach i in 1 to lbxWatchedFolder.listCount do
dirHandle := call findFirstChangeNotification(lbxWatchedFolder.itemText, false.Integer, FILE_NOTIFY_CHANGE_FILE_NAME);
handlesArray[iOffset : 4] := dirHandle.Binary;
iOffset := iOffset + 4;
endforeach;
handleCount := lbxWatchedFolder.listCount;
handlesArrayPointer := handlesArray.bufferAddress();
// Start monitoring
beginTimer(100, Timer_Continuous, 1001);
btnStartStop.caption := "Stop Spying";
else
endTimer(1001);
endif;
end;
timerEvent(eventTag: Integer) updating;
vars
result : Integer;begin
if eventTag = 1001 then
result := call waitForMultipleObjects(handleCount, handlesArrayPointer, false.Integer, 20);
if result <= lbxWatchedFolder.listCount then
app.msgBox("File change event occurred in directory '" & lbxWatchedFolder.itemText[result + 1] & "'.", "", MsgBox_OK_Only);
stopSpying();
endif;
endif;
end;
Aaron.