• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polling directory for new Files  RSS feed

 
Eddie Howard
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am just wondering if any one has a simple solution for the above problem.
Basically I would like to poll a folder to check as new files arrive and then do my processing on file.

I am just wondering if anyone has built\seen as elegant & simple solutions
Two approaches I am considering would be a simple wait statement between checking folder or creating a listener. The listener would be more elegant but less simple.

Any comments? Any alternative approaches?

Rgds
Ed
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you define "new"? If i modify an existing file, is that new? what if the file is deleted, then restored, but the contents haven't changed?

How you define "new" is going to influence what you do
 
Eddie Howard
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
My approach would be
A Identify file, open file
B Process contents of file
C Move file to processed or rejected as appropriate

So all files in folder will be processed, but once this is done they will be moved. So renaming should matter.
If same content arrive a second time with new file name, then that file will also be processed

Clear?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java's Timer is a good way to schedule periodic operations like checking for new files. As Fred pointed out, deciding what's new may be as simple as timestamp > last run or as complex as your business rules make it.

And "listener" is also a good concept, too. Rather than making the directory checker do any real work with the files it finds, have it broadcast a "New File Found" event to one or more listeners. That helps the checker and the listener both do just one thing well.

If you were going to be doing a lot of this, you could wind up with a package containing a directory checker, an interface for plug-in Strategies for deciding what's new and a listener interface that clients could implement and subscribe to new file events. You could use it for any number of purposes without ever touching the code again. That's just an old geek wandering off the path ... do the simplest thing that works for now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!