C++ Directory Watching - How to detect copy has ended

I have a folder to which files are copied. I want to watch it and process files as soon as they are copied to the directory. I can detect when a file is in the directory, whether through polling (my current implementation) or in some tests using Windows APIs from a few samples I've found online.

The problem is that I detect when the file is first created and its still being copied. This makes my program, that needs to access the file, through errors (because the file is not yet complete). How can I detect not when the copying started but when the copying ended? I'm using C++ on Windows, so the answer may be platform dependent but, if possible, I'd prefer it to be platform agnostic.

You could use either lock files or a special naming convention. The easiest is the latter and would work something like this:

Say you want to move a file named "fileA.txt" When copying it to the destination directory, instead, copy it to "fileA.txt.partial" or something like that. When the copy is complete, rename the file from "fileA.txt.partial" to "fileA.txt". So the appearance of "fileA.txt" is atomic as far as the watching program can see.

The other option as mentioned earlier is lock files. So when you copy a file named "fileA.txt", you first create a file called "fileA.txt.lock". When the copying is done, you simply delete the lock file. When the watching program see "fileA.txt", it should check if "fileA.txt.lock" exists, if it does, it can wait or revisit that file in the future as needed.

C++ Directory Watching - How to detect copy has ended, I have a folder to which files are copied. I want to watch it and process files as soon as they are copied to the directory. I can detect when a file is in the directory​,  When I am putting a large size into the attached watch folder the event raised immediately even the file copy process still not completed. I don’t want to check this by file.open method. Is there any way get notify that my file copy process into the watch folder has been completed and then my event get fire.

You should not be polling. Use FindFirstChangeNotification (http://msdn.microsoft.com/en-us/library/windows/desktop/aa364417%28v=vs.85%29.aspx) to watch a directory for changes.

Then use the Wait functions (http://msdn.microsoft.com/en-us/library/windows/desktop/ms687069%28v=vs.85%29.aspx) to wait on change notifications to happen.

Overview and examples here: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365261%28v=vs.85%29.aspx

I'm not sure how exactly file write completion can be determined. Evan Teran's answer is a good idea.

C Directory Watching - How to detect copy has ended, I want to watch it and process files as soon as they are copied to the directory. .​com/questions/9182070/c-directory-watching-how-to-detect-copy-has-ended. I am using a Windows Forms Application to monitor a directory and move the files dropped in it to another directory. At the moment it will copy the file to another directory, but when another file

You can use something like this, This is tested and working

bool IsFileDownloadComplete(const std::wstring& dir, const std::wstring& fileName) 
{
    std::wstring originalFileName = dir + fileName;
    std::wstring tempFileName = dir + L"temp";

    while(true)
    {
        int ret = rename(convertWstringToString(originalFileName).c_str(), convertWstringToString(tempFileName).c_str());
        if(ret == 0)
            break;      

        Sleep(10);
    }   

    /** File is not open. Rename to original. */
    int ret = rename(convertWstringToString(tempFileName).c_str(), convertWstringToString(originalFileName).c_str());
    if(ret != 0)
        throw std::exception("File rename failed"); 

    return true;
}

FileSystemWatcher Class (System.IO), C Directory Watching - How to detect copy has ended. Question. I have a folder to which files are copied. I want to watch it and process files as soon as they are  Instantiate the FileSystemWatcher class by creating a object. Set the directory to watch by setting the path property. FileSystemWatcher fwatcher = new FileSystemWatcher(); fwatcher.Path = txtFolder.Text; Type of changes to watch is set by the property NotifyFilter.

Obtaining Directory Change Notifications, The following example creates a FileSystemWatcher to watch the directory that the contents of folders have been moved or copied into a watched folder, Gets or sets the filter string used to determine what files are monitored in a directory. Filters Ends the initialization of a FileSystemWatcher used on a form or used by​  Once the FileSystemWatcher is set to watch a file or folder, it will keep monitoring it till the end of the application. Setting the m_Watcher value to null will not stop it from monitoring. To stop the watcher while the application is still running, we need to stop it from raising events. This is done as follows.

How to check progress of running cp?, An application can monitor the contents of a directory and its When something changes within the directory being watched, the read operation is completed. C​++. Copy. #include <windows.h> #include <stdlib.h> #include n"); ExitProcess(​GetLastError()); } // Make a final validation check on our  This will show you how to find if someone have been accessing your computer physically, and not remotely. Enjoy! Watch also: How to find if someone hacked your computer HD https://www.youtube.com

Watching a Directory for Changes (The Java™ Tutorials > Essential , And you get the percentage of data copied by comparing the two value, that's it I don't think Linux has this SIGINFO mechanism, and don't see anything in the in which the files inside a given directory are copied is essentially unpredictable This tool can be described as a Tiny, Dirty, Linux-and-OSX-Only C command  Say you want to move a file named "fileA.txt" When copying it to the destination directory, instead, copy it to "fileA.txt.partial" or something like that. When the copy is complete, rename the file from "fileA.txt.partial" to "fileA.txt". So the appearance of "fileA.txt" is atomic as far as the watching program can see.

Comments
  • I found this question just a few minutes ago: stackoverflow.com/questions/559659/… It's very similar to my problem but the "approved" solution is far from being elegant and functional.
  • If your code will be running in an administrative context, i.e., as a system service, you could use a change journal. See msdn.microsoft.com/en-us/library/windows/desktop/…
  • Considering the possibility of things like naive copying using fread()/fwrite() i don't think there is a general solution. Not quite robust, but maybe by tracking open handles to the files?
  • Well that's a workaround and might work on other environments. But on this case, I'm talking about files being copied by other programs/via the file explorer to a folder being watched by my program. In that case this approach is very hard to accomplish.
  • Not sure it's possible if you have no control over how the files are copied.
  • It's exactly because polling is not a good idea that I'm trying to replace it with something better ;) And yeah! I found those methods and I already have a solution based on this: code.google.com/p/simplefilewatcher. The problem is to determine file write completion. That library (that I have already modified slightly) uses ReadDirectoryChangesW that I believe it's even more powerful than FindFirstChangeNotification.
  • On a side note, I just wonder how Dropbox (for example) deals with this kind of issues.
  • You could try to open the file for exclusive write, which will fail until the copy is finished. There's probably a way to block on that condition, instead of polling it, but I don't know it offhand.
  • Yeap. I ended up doing something like that, with a long polling time (like 3 or 5 seconds). It's far from perfect but the Windows API doesn't allow something better.