Bv202
Bv202

Reputation: 4044

Windows - get warned when a file is created

I'm not sure if what I'm asking is possible, but I would like to do the following: When a file is created in a certain folder (Windows), my program should respond. I'd like to let Windows call a callback method when a file is created.

Another option is of course just use a loop and constantly check if a new file is in the folder, but I'd like to know it instantly, so a callback method would be much more efficient.

Is this possible? The language is not important, although Java is preferred.

Upvotes: 5

Views: 131

Answers (3)

Petr Mensik
Petr Mensik

Reputation: 27506

If you are not bound to Java, then you could use very convenient FileSystemWatcher in C# or VisualBasic. It will allow you to watch all kinds of events which can occur in folder and it's quite easy to implement it.

Upvotes: 0

Ludwig Magnusson
Ludwig Magnusson

Reputation: 14399

Commons IO contains a FileAlterationListener wich has a onDirectoryChangemethod. Can be an alternative if Java 1.7 is not available.

Upvotes: 1

assylias
assylias

Reputation: 328619

With Java nio 2 (available in Java 1.7 +), you can "watch" a directory and get notified when that directory changes.

The method proposed in the tutorial linked above uses the WatchService API.

Upvotes: 7

Related Questions