tags:

views:

60

answers:

4

Is it possible to copy a new file created by third party application inside a directory automatically on windows?

It goes like:

Third party process 'P' creates a new temporary file 'F' inside a directory 'D'. Whenever this file F is created by the process P, I want to copy this file F into another directory D2. Additional problem is that file F gets deleted by the process P after some time. So cron job won't help.

I think, I need to trap 'new file created' event somehow, if any such thing exists.

A: 

You should create an application that constantly monitors that directory for new files, and moves them as soon as they are created.

There is no "new file created" event to monitor.

snicker
@snicker: That's what I thought. Is there a way to piggyback a process with another one? I mean when process P starts I can start my own process to watch and it ends with P.
understack
@snicker: how can I detect "moves them as soon as they are created"? I obvious way would be to check directory size after every few seconds.
understack
You can start your process and terminate if the other process does not exist. In C#, specifically, this can be accomplished by first finding the process by using System.Diagnostics.Process.GetProcesses and then monitoring its state in your main application loop. See http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getprocesses.aspx
snicker
You simply keep a running tally of what was in the directory when you started, and if something new appears, move it. You have to do this at intervals you feel are small enough to capture your elusive file.
snicker
Bravax's answer is much easier for C#.
snicker
+1  A: 

The easiest way would be to write a program in c# which uses the FileSystemWatcher class.

See: FileSystemWatcher

Then catch the created event, and copy the file.

Bravax
@Bravax: Is there a way to detect 'process killed/terminated'? Then I can terminate my program as soon as third party application quits.
understack
snicker has answered this.
understack
Though I've accepted this answer since technically it's better but personally I'm going with Jonathan's dirty solution.
understack
A: 

You could use .NET's FileSystemWatcher class to monitor a directory for new files.

If you're looking for a more native solution, check out the FindFirstChangeNotification family of Win32 functions.

David Grant
+1  A: 

Quick and Dirty: You can run a simple .bat file that contains an infinite loop. Inside the loop copy (with overwrite /y) the content of the directory, and then use the sleep command to rest for a while.

This solution is quick to develop and test.

Jonathan
This one is dirty but awesome
understack
Thanks!! My solution looks like me :D
Jonathan