WatchDir (watch a directory)

function WatchDir(path: string, recursive: boolean)

This FsWatcher method adds a directory/folder to the list of directories/folders “watched” by the local file-system watcher.

The path parameter specifies a directory path to be watched.

The recursive parameter specifies whether or not the FsWatcher should also watch all sub-folders of the specified folder.

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;
    }
  }
}