Differences
This shows you the differences between the selected revisions of the page.
library_example_watch_for_changes 2016-09-06 | library_example_watch_for_changes 2022-06-16 (current) | ||
Line 1: | Line 1: | ||
====== Watching for changes in SFTP/FTP server ====== | ====== Watching for changes in SFTP/FTP server ====== | ||
- | Neither SFTP nor FTP protocol have any mechanism to notify a client about changes in a remote folder. The only solution to detect changes is to periodically enumerate remote directory tree and find differences. It's easy to implement with use of ''[[library_session_enumerateremotefiles|Session.EnumerateRemoteFiles]]'' method and ''[[https://technet.microsoft.com/en-us/library/hh849941.aspx|Compare-Object]]'' cmdlet in PowerShell or ''[[msdn>system.linq.enumerable.except|Enumerable.Except]]'' LINQ method in C#/%%VB.NET%%. | + | Neither SFTP nor FTP protocol have any mechanism to notify a client about changes in a remote folder. The only solution to detect changes is to periodically enumerate remote directory tree and find differences. It's easy to implement with use of ''[[library_session_enumerateremotefiles|Session.EnumerateRemoteFiles]]'' method and ''[[ps>microsoft.powershell.utility/compare-object|Compare-Object]]'' cmdlet in PowerShell or ''[[dotnet>system.linq.enumerable.except|Enumerable.Except]]'' LINQ method in C#/%%VB.NET%%. |
//Note that, if you want to download the changes, you do not need to check for changes yourself, just run ''[[library_session_synchronizedirectories|Session.SynchronizeDirectories]]'' in a loop. You can use the [[library_example_keep_local_directory_up_to_date|Keep local directory up to date]] extension.// | //Note that, if you want to download the changes, you do not need to check for changes yourself, just run ''[[library_session_synchronizedirectories|Session.SynchronizeDirectories]]'' in a loop. You can use the [[library_example_keep_local_directory_up_to_date|Keep local directory up to date]] extension.// | ||
Line 9: | Line 9: | ||
<code powershell> | <code powershell> | ||
param ( | param ( | ||
- | # Use Generate URL function to obtain a value for -sessionUrl parameter. | + | # Use Generate Session URL function to obtain a value for -sessionUrl parameter. |
- | $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xx-xx-xx@example.com/", | + | $sessionUrl = "sftp://user:mypassword;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/", |
$remotePath = "/home/user" | $remotePath = "/home/user" | ||
) | ) | ||
Line 36: | Line 36: | ||
# Collect file list | # Collect file list | ||
$files = | $files = | ||
- | $session.EnumerateRemoteFiles($remotePath, "*.*", [WinSCP.EnumerationOptions]::AllDirectories) | | + | $session.EnumerateRemoteFiles( |
+ | ····················$remotePath, "*.*", [WinSCP.EnumerationOptions]::AllDirectories) | | ||
Select-Object -ExpandProperty FullName | Select-Object -ExpandProperty FullName | ||
Line 42: | Line 43: | ||
{ | { | ||
# In the first round, just print number of files found | # In the first round, just print number of files found | ||
- | Write-Host ("Found {0} files" -f $files.Count) | + | Write-Host "Found $($files.Count) files" |
} | } | ||
else | else | ||
Line 82: | Line 83: | ||
} | } | ||
} | } | ||
- | catch [Exception] | + | catch |
{ | { | ||
- | Write-Host ("Error: {0}" -f $_.Exception.Message) | + | Write-Host "Error: $($_.Exception.Message)" |
exit 1 | exit 1 | ||
} | } | ||
Line 111: | Line 112: | ||
UserName = "user", | UserName = "user", | ||
Password = "password", | Password = "password", | ||
- | SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx", | + | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...", |
}; | }; | ||
Line 127: | Line 128: | ||
// Collect file list | // Collect file list | ||
List<string> files = | List<string> files = | ||
- | session.EnumerateRemoteFiles(remotePath, "*.*", EnumerationOptions.AllDirectories) | + | session.EnumerateRemoteFiles( |
+ | ····························remotePath, "*.*", EnumerationOptions.AllDirectories) | ||
.Select(fileInfo => fileInfo.FullName) | .Select(fileInfo => fileInfo.FullName) | ||
.ToList(); | .ToList(); |