Events (to watch for)¶
{
NotifyCreate: boolean // default: true
NotifyRemove: boolean // default: true
NotifyWrite: boolean // default: true
NotifyRename: boolean // default: true
NofityChmod: boolean // default: true
}
These 4 properties of the FsWatcher object determine which local file-system events will be included in the watcher’s notifications and which ones won’t. By default they are all set to true, so unless you want to disable some of them, you don’t need to set/reset them in your code.
NotifyCreate
: used to receive notifications when an object (a file or a folder) is created inside of the watched path(s).
NotifyRemove
: used to receive notifications when an object (a file or a folder) is deleted inside of the watched path(s).
NotifyWrite
: used to receive notifications when an object (typically a file) is modified (written-to) inside of the watched path(s).
NotifyRename
: used to receive notifications when an object (a file or a folder) is renamed inside of the watched path(s).
NotifyChmod
: used to receive notifications when an object (a file or a folder) metadata is modified.
Example:
{
ConsoleFeedback = true;
// Create the file-system watcher
watcher = new FsWatcher();
// Set file-system watcher properties
watcher.DelayBySeconds = 300;
watcher.NotifyRename = false;
watcher.NotifyRemove = false;
watcher.NotifyChmod = false;
watcher.InclusionFilter = ['*.*'];
watcher.ExclusionFilter = ['notes.txt', 'budget.xlsx'];
watcher.WatchDir('C:\\TestFolder', false);
// Start the file-system watcher
watcher.Start();
while (true) {
Sleep(500);
if (HaltSignalReceived()) {
break;
}
// Acquire the list of pending event that we need to process
evt = watcher.Events()
// Do we have at least 1 event to process?
if (evt.length > 0) {
// We only connect to the server IF there are events to be processes
var scli = new SftpClient();
scli.Host = 'your.sftpserver.com:22';
scli.User = 'your_username';
scli.Pass = 'your_password';
scli.Options.UploadPolicy = AlwaysOverwrite;
if (scli.Connect()) {
// Cycle over all pending events...
for (var i = 0; i < evt.length; i++) {
if (evt[i].Event == 'WRITE') {
// If it is a WRITE event (new or modified file) let's upload it to the server
scli.UploadWithPath(evt[i].Object, '/destinationpath', 1);
}
}
// Do not forget to close the connection
scli.Close();
}
scli = null;
}
}
}