Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 21st, 2006, 3:28 PM   #1
Marc_piecko
Newbie
 
Join Date: May 2006
Posts: 1
Rep Power: 0 Marc_piecko is on a distinguished road
Cool How to start a program upon file creation ?

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
Marc_piecko is offline   Reply With Quote
Old May 21st, 2006, 4:11 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 21st, 2006, 7:34 PM   #3
volatile
Newbie
 
Join Date: Nov 2005
Posts: 18
Rep Power: 0 volatile is on a distinguished road
#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.
volatile is offline   Reply With Quote
Old May 21st, 2006, 8:06 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 22nd, 2006, 4:52 AM   #5
volatile
Newbie
 
Join Date: Nov 2005
Posts: 18
Rep Power: 0 volatile is on a distinguished road
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.
volatile is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC