FileMonitor Manual

Table of Contents

About

FileMonitor class provides means to be notified about changes of a file or a folder. Simply call one of the "Watch" functions to start monitoring. These functions get passed a pointer to a callback function, which needs to be implemented and is called on every change.

Warning
FileMonitor class is deprecated and will be replaced in a future release.
// This example demonstrates setting up two FileMonitors.
// notifying about changes of a file and in a folder.
// Upon execution the user will be asked to choose a file.
// The example will then watch the chosen file and the folder it is contained in.
// Every change to folder or file will be printed to the Console.
static Int32 WatchFolderCallback(const Filename& item, Int flags, void* userdata)
{
// Will be called on any change of the folder (also on changes of files inside the folder).
// Note: Creation of a new file might trigger two calls of this callback.
GePrint("Folder change: " + item.GetString());
return 0;
}
static Int32 WatchFileCallback(const Filename& item, Int flags, void* userdata)
{
GePrint("File change: " + item.GetString());
return 0;
}
class FileMonitorDemo : public CommandData
{
INSTANCEOF(FileMonitorDemo, CommandData)
private:
Filename _fnFolder; // Stores the filename of the currently watched folder.
Filename _fnFile; // Stores the filename of the currently watched file.
public:
virtual Bool Execute(BaseDocument* doc)
{
// If there's a path stored in _fnFolder, FileMonitor is already watching a folder/file from a previous run.
if (_fnFolder.Content())
{
// Stop watching folder and file.
if (FileMonitor::DontWatchFolder(_fnFolder, WatchFolderCallback, nullptr))
GePrint("Stopped watching folder: " + _fnFolder.GetString());
else
GePrint("Failed stop watching folder: " + _fnFolder.GetString());
if (FileMonitor::DontWatchFile(_fnFile, WatchFileCallback, nullptr))
GePrint("Stopped watching file: " + _fnFile.GetString());
else
GePrint("Failed stop watching file: " + _fnFile.GetString());
// Reset the member variable, so next time user can select another file.
_fnFolder.SetString("");
_fnFile.SetString("");
return true;
}
// Let user select a file and folder to watch (folder will be the one that contains the selected file).
if (!_fnFile.FileSelect(FILESELECTTYPE_ANYTHING, FILESELECT_LOAD, "Select a file to watch..."))
return false;
_fnFolder = _fnFile.GetDirectory();
// Start watching folder and file.
if (FileMonitor::WatchFolder(_fnFolder, WatchFolderCallback, nullptr))
GePrint("Started watching folder: " + _fnFolder.GetString());
else
GePrint("Failed to watch folder: " + _fnFolder.GetString());
if (FileMonitor::WatchFile(_fnFile, WatchFileCallback, nullptr))
GePrint("Started watching folder: " + _fnFile.GetString());
else
GePrint("Failed to watch folder: " + _fnFile.GetString());
return true;
};
static FileMonitorDemo* Alloc() { return NewObjClear(FileMonitorDemo); }
};

Watch

Starting and stopping monitoring of files and folders is easily accomplished with the following functions:

Callback

The above functions get passed a function pointer, the pointed function will be called upon change. The prototype of such a function has to match the FileMonitorCallback type, which looks like this:

Int32 (*FileMonitorCallback) (const Filename& item, Int flags, void* userdata)

Further Reading