ofxIO how to know if file is ready to be read

#1

Hello Forum.

Using ofxIO and it’s directory watcher.
void onDirectoryWatcherItemAdded(const ofxIO::DirectoryWatcherManager::DirectoryEvent& evt)

I notice that just because the file appears in the folder does not mean it is ready to be read in to the app. I guess the copying process is not completely done and so file loading fails.

How would I work around this?
I know I could take the current time and just wait x seconds. But it feel not elegant.

thx.

3 Likes
#2

If you have control over file writing, you could write them to a different directory or an ignored temporary filename then do a move or rename operation that will put them in the watched directory.

If you don’t have control over how files are written, on each new file detection, you might measure the file size, and immediately copy the filename and size to a vector. Then during each subsequent ofApp::update loop, you can iterate the list of files, and check their current size vs the last measured size. When the current size and last measured size are equal (and you’re confident you aren’t measuring too quickly to detect a slow change), you can then load the files.

You don’t have to use an update() loop for this, but it would keep your program from blocking during each write.

#3

Also, if you figure out a nice way to do this, it would be a nice addition to ofxIO as it can be a common problem. Feel free to PR :slight_smile:

2 Likes