Watching for new files

  • Thread starter Luc The Perverse
  • Start date
L

Luc The Perverse

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.
 
A

Andrey Kuznetsov

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.

the simplest way is to keep directory empty.
Just move files after upload to another directory
 
T

Tony Morris

Luc The Perverse said:
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.

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").
 
L

Luc The Perverse

Tony Morris said:
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").

That's what I was afraid of. I've never been to good with any of this
windows messaging crap.
 
L

Luc The Perverse

Andrey Kuznetsov said:
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.
 
G

Gordon Beaton

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
 
T

Thomas Weidenfeller

Luc said:
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
 
A

Andrey Kuznetsov

It is likely I can figure out the FTP itself on my own - but polling
That is a good idea. Is there a way to check to make sure the file isn't
open...

File.canWrite();
 
J

jcsnippets.atspace.com

Luc The Perverse said:
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.

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
 
L

Luc The Perverse

jcsnippets.atspace.com said:
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.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,795
Messages
2,569,644
Members
45,359
Latest member
1854578

Latest Threads

Top