Events (polling for)¶
function Events(): []FsWatcherEvent
FsWatcherEvent = {
TimeStamp: Date // timestamp of when the event happened
Event: string // watcher event as string: CREATE, REMOVE, WRITE, RENAME, CHMOD
Object: string // the remote object (file/directory) affected by Event
}
This FsWatcher method is designed to be called within the scope of an endless loop, to keep the script running forever (unless terminated by an admin or an OS signal). Each time this method is called, it returns an array of pending, to-be-handled, file system notifications.
Every call to this method will also clear the pending notifications, so all events that are left un-handled will not be notified again.
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 = ['*.*']; // include everything
watcher.ExclusionFilter = ['notes.txt']; // exclude 1 file by name
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;
}
}
}