Detecting file in directory.

For questions and postings not covered by the other forums
ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Detecting file in directory.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:19 pm

by Cass >> Fri, 30 May 2008 8:12:25 GMT

Hi,

Is there any event or other way of detecting when a file is placed into a directory? I.e. The folder will be empty for the majority of the time, but then when a file is placed into it a program runs on that file. Is this possible to do in JADE? Ideally, this should be done using some kind of notification rather than having something watch the folder, as there could be days or even weeks between each file being there!

Thanks in advance!

Rich

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Detecting file in directory.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:19 pm

by cnwjhp1 >> Mon, 2 Jun 2008 22:32:38 GMT

It is possible to get notifications of such things from Windows api's, but a timer loop is really not much of an overhead.

In a project a few years ago, I had a background app on a 6 second timer. When the timer fired, it read 150 objects from the database and checked 150 file folders (folder names were in the database). There was no noticeable cpu consumption, and that was a 500 mhz cpu! So one or two checks of hard-coded folders on a timer won't hurt anything.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Detecting file in directory.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:19 pm

by BeeJay >> Tue, 3 Jun 2008 0:15:51 GMT

Another advantage of doing it yourself via timers, rather than using OS provided API calls, is that you are more OS independent for your application. You can then more easily support users running your application on any of Jade's supported presentation client and server OSes.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Detecting file in directory.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:19 pm

by Cass >> Tue, 3 Jun 2008 8:14:50 GMT

Thanks both for your replies. I shall look into using timers, it was the processing overheads that i was worried about but from what you are both saying it doesn't seem to provide any. Do either of you have any other pointers from your experience with using timers in this manner? Is there anything i should avoid or definitely do?

Plus, the file that is placed in the folder will have a timestamp within its name, therefore each one will be named differently. In the past i have only ever hard coded the filename to open, is there any way of opening a file irrelevant of its name? There will only ever be the single required file in the folder.


Thanks again!

Rich

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Detecting file in directory.

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.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Detecting file in directory.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:19 pm

by cnwjhp1 >> Tue, 3 Jun 2008 21:46:19 GMT

Here is a code snippet from my app.

vars
file:FileNode;
s,result,outName:String;
retry:Integer;begin

folder.fileName:=(app.dataPath&"\"&fb.fromDir).toLower;
folder.usePresentationFileSystem:=false;
if folder.isAvailable then
foreach file in folder.files where file.isKindOf(File) do
s:=file.fileName.toLower;
file.File.mode:=File.Mode_Input;
file.File.kind:=File.Kind_Binary;
file.usePresentationFileSystem:=false;
file.File.shareMode:=File.Share_Exclusive;

Note that FileFolder::files() returns a FileNode object, not a File object. Depending on what you do with it, you may prefer to create a File object instead of using the FileNode object like I did here.

You should definitely avoid leaking transients! If you create File or FileFolder objects, be sure to delete them.

ConvertFromOldNGs
Posts: 5321
Joined: Wed Aug 05, 2009 5:19 pm

Re: Detecting file in directory.

Postby ConvertFromOldNGs » Fri Aug 07, 2009 1:19 pm

by John Munro >> Mon, 16 Jun 2008 14:12:19 GMT

You could import System.IO.FileSystemWatcher from .NET


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 19 guests