FsWatcher object¶
FsWatcher() // object constructor
This function creates and returns a new FsWatcher
(local file-system watcher) object.
This object can be used later in the script to be constantly notified of the desired changes
to the observed (watched) directory/folder, and optionally its sub-folders.
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;
}
}
}