Delay notifications¶
.DelayBySeconds(seconds: number) // default: 0 (no delay)
This property of the FsWatcher object causes a delay of the specified number of seconds to all file-system event notifications.
This can be extremely useful to allow the OS enough time to complete file operations before the FsWatcher notifies our script and triggers the execution of a file transfer operation, for example.
Example:
{
ConsoleFeedback = true;
// Create the file-system watcher
watcher = new FsWatcher();
// Set file-system watcher properties
watcher.DelayBySeconds = 300; // 5 minute delay
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;
}
}
}