CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2002
    Posts
    48

    Cool help on monitor the folder

    Hi, guru,

    I'm a VC beginner and now building a FTP client.

    I want to build a programme to monitor the folder like "d:\temp". If a new file is created on the folder, the ftp it to the server.

    My question is:

    what event or function I need to check there is a new file created on the folder?

    rgds,

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Look out for FindFirstChangeNotification(), FindNextChangeNotification(), FindCloseChangeNotification() and WaitForSingleObject().

  3. #3
    Join Date
    Dec 2002
    Posts
    48

    the question in programme

    Hi, friend,

    Thanks for the reply.

    I look up some sample of MS and try to use it. But there is a dead loop.

    I build the function:
    OnButtonClick()
    {
    function1();
    function2();
    }

    The function1() is OK. For function2():
    void function2()
    {
    DWORD dwWaitStatus;
    HANDLE dwChangeHandles;

    // Watch the m_strMonitorFolderName directory for file creation
    dwChangeHandles = FindFirstChangeNotification(
    m_strMonitorFolderName, // directory to watch
    FALSE, // do not watch the subtree
    FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes

    if (dwChangeHandles == INVALID_HANDLE_VALUE)
    ExitProcess(GetLastError());

    while (TRUE)
    {

    // Wait for notification.
    dwWaitStatus = WaitForSingleObject( dwChangeHandles, 0);

    if(dwWaitStatus==WAIT_OBJECT_0)
    {
    function3();
    if ( FindNextChangeNotification( dwChangeHandles) == FALSE )
    ExitProcess(GetLastError());
    }
    }

    Question:
    1. Because I want the programme monitor the folder after strat up, so I use while(true) in function2. But there seems the programme is dead lock after call function2.
    2. I want to monitor whether there is a new file created, is that right to use
    dwChangeHandles = FindFirstChangeNotification(
    m_strMonitorFolderName, // directory to watch
    FALSE, // do not watch the subtree
    FILE_NOTIFY_CHANGE_FILE_NAME); // watch file name changes
    3. I want to programme react immediatly, so whether it is right to use
    dwWaitStatus = WaitForSingleObject( dwChangeHandles, 0);

    cheers!

  4. #4
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Normally, when WaitForSingleObject() is being used, the current thread will be suspend until timeout or the event occurs. So, to prevent the program from being frozen, it is quite common to spawn-off a thread for the monitoring purpose. This is the reason why your program dead lock.

    I am not sure if FILE_NOTIFY_CHANGE_FILE_NAME works but I had tried FILE_NOTIFY_CHANGE_LAST_WRITE before.


    Code:
    AfxBeginThread(NotifyThreadProc, 0)
    
    UINT NotifyThreadProc(LPVOID pParam)
    {
    	BOOL bKill = FALSE;
    	HANDLE hMonitor = NULL;
    
    
    	// First time for tracking change notification.
    	hMonitor = FindFirstChangeNotification(path, FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE );
    
    	while(!bKill)
    	{
    
    		//
    		// Wait for the change notification.
    		//
    		DWORD dwWait = WaitForSingleObject(hMonitor, INFINITE);
    	
    		// Handles the return result.
    		switch(dwWait)
    		{
    		case WAIT_OBJECT_0:
    			// Detected a change in the folder.
    			// Do something. 
    
    			break;
    
    		case WAIT_ABANDONED:
    			// Perform error handling.
    			break;
    
    		case WAIT_TIMEOUT:
    			// Perform error handling.
    			break;
    
    		case WAIT_FAILED:
    			// Perform error handling.
    			break;
    
    		default:
    			break;
    		}
    
    		// Subsequent change notification tracking.
    		FindNextChangeNotification(hMonitor );
    	}
    
    	return 0;	
    }

  5. #5
    Join Date
    Dec 2002
    Posts
    48

    Thumbs up Really Thanks!

    Cheers! I know a lot because I'm a beginer of VC.

  6. #6
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    I'm glad it helps.

  7. #7
    Join Date
    Aug 1999
    Location
    Romania, Bucharest
    Posts
    253
    hi
    i found that thread interesting but i need some advice now

    it is possible to know the file that notify me?
    i want to know what file was opened from a folder and when

    can you help me regarding this problem?
    thank you in advance for your help

  8. #8
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    ReadDirectoryChangesW().

    I had never use this API before but I believe this is what you are looking for. Please tell me if it works. Thanks.

  9. #9
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Hi, I just found a sample from MSDN.

    http://msdn.microsoft.com/library/de...hangeswapi.asp

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured