Is there a better way to watch a directory for new files than to simply poll
it and compare against a list?
I have a security Cam programming running on a windows box, and I would like
to watch for appearance of new files and then quickly upload them to an FTP
server.
A unique image file will be generated every time movement is detected on the
camera.
It is likely I can figure out the FTP itself on my own - but polling every
few seconds for new files sounds less than desirable.

Signature
LTP
:)
Andrey Kuznetsov - 01 Feb 2006 22:31 GMT
> Is there a better way to watch a directory for new files than to simply
> poll it and compare against a list?
[quoted text clipped - 8 lines]
> It is likely I can figure out the FTP itself on my own - but polling every
> few seconds for new files sounds less than desirable.
the simplest way is to keep directory empty.
Just move files after upload to another directory

Signature
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
Luc The Perverse - 02 Feb 2006 02:55 GMT
>> Is there a better way to watch a directory for new files than to simply
>> poll it and compare against a list?
[quoted text clipped - 11 lines]
> the simplest way is to keep directory empty.
> Just move files after upload to another directory
That is a good idea. Is there a way to check to make sure the file isn't
open - since it is at least possible, if not likely that a file will be
detected while it is in the process of being writen.
Perhaps moving it and copying it can be part of the same operation - and if
moving fails, then it can be assumed that the file is not yet ready to be
copied. This is still polling - but requires no memory of which files have
been transfered and resolves the issue of trying to transfer files which are
still open.
--
LTP
:)
Andrey Kuznetsov - 02 Feb 2006 10:58 GMT
>>> It is likely I can figure out the FTP itself on my own - but polling
>>> every few seconds for new files sounds less than desirable.
[quoted text clipped - 4 lines]
> That is a good idea. Is there a way to check to make sure the file isn't
> open...
File.canWrite();

Signature
Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities
Tony Morris - 01 Feb 2006 23:16 GMT
> Is there a better way to watch a directory for new files than to simply poll
> it and compare against a list?
[quoted text clipped - 10 lines]
>
> :)
Not using the Java platform.
Other platforms permit you to register a callback for notification of
changes - this is the correct way of avoiding a busy/wait (or as you put it,
"polling").

Signature
Tony Morris
http://tmorris.net/
Luc The Perverse - 02 Feb 2006 02:53 GMT
>> Is there a better way to watch a directory for new files than to simply
> poll
[quoted text clipped - 21 lines]
> it,
> "polling").
That's what I was afraid of. I've never been to good with any of this
windows messaging crap.

Signature
LTP
:)
Gordon Beaton - 02 Feb 2006 08:00 GMT
>> Not using the Java platform.
>> Other platforms permit you to register a callback for notification of
[quoted text clipped - 4 lines]
> That's what I was afraid of. I've never been to good with any of this
> windows messaging crap.
Search for ReadDirectoryChangesW on Windows, or FAM on Linux, etc. At
any rate it's system specific and will require JNI or an external
helper.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Thomas Weidenfeller - 02 Feb 2006 08:39 GMT
> Is there a better way to watch a directory for new files than to simply poll
> it and compare against a list?
>
> I have a security Cam programming running on a windows box, and I would like
> to watch for appearance of new files and then quickly upload them to an FTP
> server.
The real problem might not be to figure out if there is suddenly a new
file, but if it has been completely written by the camera. If you start
to early with the download of the file you might miss parts of the file.
So you probably don't want to "quickly upload" them.
> A unique image file will be generated every time movement is detected on the
> camera.
You probably need to wait until there are two or more such files, and
then upload all except the latest one (which might be incomplete). This
assumes that the camera only starts to write a new file after it has
completed the previous one. Which is likely.
Or you need to have some other way to check the integrity of a file.
> It is likely I can figure out the FTP itself on my own - but polling every
> few seconds for new files sounds less than desirable.
If the camera doesn't provide any notification mechanism this is
probably the only way to do it.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Roedy Green - 02 Feb 2006 11:09 GMT
On Wed, 1 Feb 2006 15:15:46 -0700, "Luc The Perverse"
<sll_noSpamlicious_z_XXX_m@cc.usu.edu> wrote, quoted or indirectly
quoted someone who said :
>Is there a better way to watch a directory for new files than to simply poll
>it and compare against a list?
See http://mindprod.com/projects/filefinder.html
That is a student project that tackles your same problem in a more
generic way.
In particular check the reference to Sysinternals Filemon.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
jcsnippets.atspace.com - 02 Feb 2006 20:37 GMT
> Is there a better way to watch a directory for new files than to simply poll
> it and compare against a list?
[quoted text clipped - 8 lines]
> It is likely I can figure out the FTP itself on my own - but polling every
> few seconds for new files sounds less than desirable.
How about querying file.lastModified() and keeping the timestamp of the
latest file? This way you:
- don't have to keep track of all files to compare
- know when a new file has arrived (by looping through the directory for
files with a higher lastModified() value)
- can repeat this forever if you adjust lastModified() to the newest file
after the copy process has ended
Just a thought. I don't see any problems with it at the moment, but if
someone else does, I'd like to know them out of curiosity.
Kind regards,
JC
--
http://jcsnippets.atspace.com
a collection of source code, tips and tricks
Luc The Perverse - 02 Feb 2006 23:43 GMT
>> Is there a better way to watch a directory for new files than to simply
> poll
[quoted text clipped - 24 lines]
> Just a thought. I don't see any problems with it at the moment, but if
> someone else does, I'd like to know them out of curiosity.
If would require a discretization since 1 second is the minimal amount of
time.
This is one possibility that I did look at.
It wouldn't be too hard as long as new files weren't being added in the
second that you are x-ferring. This is easy if you put in a delay
--
LTP
:)