rgcosma has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I am trying to watch over a folder for uploads, and when one is finished process each file in some way. Using Win32::FileSystem::Watcher now, I see for each file added three events: FILE_ACTION_ADDED, FILE_ACTION_MODIFIED when writing begins and another FILE_ACTION_MODIFIED when the client finishes the upload. Counting MODIFIED and acting on the second one is cumbersome and might lead to errors, is there some way to get a FILE_CLOSED event? I do realize this will not apply to paused/resumed uploads or torrents, but this is not a concern yet.

Replies are listed 'Best First'.
Re: monitoring file changes (win32)
by Corion (Patriarch) on Sep 26, 2009 at 18:11 UTC

    It seems there is no way on Windows to notify you that a file (handle) has been closed, as there might be multiple handles open to the same file. I guess the closest you'll get is Re: how to permanently monitor a directory, which will notify you of "no changes" after a certain amount of time has passed, in addition to other changes.

      Hello and thank you for the hint. The solution so far seems to be to thread the application, run $watcher->get_results() every 10 seconds, stat() each file and if the modification date is less than localtime()-10 ignore it. It should be picked by the next run. On to reading how threads work in Win32..
      After some more experimenting, I believe an apology is needed for the second point: all my tests were done in virtual machines - Virtualbox and Parallels. I have now tried the same using Microsoft's .Net System.IO.FilesystemWatcher and noticed the same problems. Something in the way those emulators handle filesystem changes causes events to not be triggered. My conclusion so far is that it's not a FS::W issue. POE::C::DW:O does work in the same setups though, this might be because it doesn't use Win32::ChangeNotify
      Any hints on where I should move this topic would be greatly appreciated.