![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2006
Posts: 1
Rep Power: 0
![]() |
I need to start a special program every time a file is created into a specific directory ...how do I do that...
The files contains parameters, that the programs use to generate a picture of a 3D model...once this is done the programs ends and the parameter file is deleted. Any help Thanks |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You have to have a program already running, possibly as a service, possibly as part of the application that creates the file. Possibly it could be spawned as a function of elapsed time, and examine that directory for modified files. In the latter case, there's still something already running, even if it's a hardware based interrupt. Either something like that, or get a fairy godmother.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2005
Posts: 18
Rep Power: 0
![]() |
#include <stdio.h>
#include "windows.h"
int main()
{
HANDLE change_handle;
DWORD wait;
char *directory_to_watch = "C:\\my_precious";
change_handle =
FindFirstChangeNotification(directory_to_watch, false, FILE_NOTIFY_CHANGE_FILE_NAME);
if(change_handle == INVALID_HANDLE_VALUE)
exit( GetLastError() );
for(;;)
{
// Block and wait for file creation
wait = WaitForSingleObject(change_handle, INFINITE);
if( wait == WAIT_OBJECT_0 )
{
// A file was renamed, created, or deleted
printf("%s %s\n", "A file was renamed, created, or deleted in ", directory_to_watch);
// *******PUT YOUR CODE HERE **********
if( FindNextChangeNotification(change_handle) == 0 )
exit( GetLastError() );
}
else if(wait == WAIT_FAILED)
exit( GetLastError() );
}
}This is how I would do it. If you run this program it will print "A file was renamed, created, or deleted in " every time you do some shite in c:\my_precious. The program does not loop and check for changes in the directory, but instead it blocks (sits and waits) for it to happen. This way it does not take up any CPU. Just add your code above. If a file was created, check the file type, if its the right stuff, read the parameters and blah blah.... Please take notice in the fact that this program is a forever loop. After you've waited for directory changes and then taken action, it will jump back up and do it all over again. You might want to change WaitForSingleObject with WaitForMultipleObjects. That way you may wait for another signal and quit if its met, perhaps a menu event. You may create a taskbar icon with a 'quit' choice. Hope this helps. Well, I'd better get back to my reading. I have a physics exam tomorrow. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Very nice. At the OP: that is your file that is "already running." The amount of time that it might use, cpu-wise, depends precisely upon how your system is implemented. You didn't mention your OS (you always should; since you're new, I recommend you read the forum's rules/FAQ and a "How to Post ..." thread); that one's Windows.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Nov 2005
Posts: 18
Rep Power: 0
![]() |
Yup. I failed to mention which OS my code was for. The rules/faq is read.
If you're writing for linux you might want to look up fcntl in the manpages, or maybe inotify, wich is better(but only available as of kernel 2.6.something). fcntl uses signals(SIGIO) while inotify can be monitored using select, poll or epoll. Theres also dnotify(which was replaced by inotify), but it requires the file to be open to watch it. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|