Differences
This shows you the differences between the selected revisions of the page.
script_download_most_recent_file 2017-02-20 | script_download_most_recent_file 2022-06-16 (current) | ||
Line 2: | Line 2: | ||
===== [[library]] Using WinSCP .NET Assembly ===== | ===== [[library]] Using WinSCP .NET Assembly ===== | ||
+ | |||
==== [[powershell]] PowerShell ==== | ==== [[powershell]] PowerShell ==== | ||
The following example uses [[library|WinSCP .NET assembly]] from a [[library_powershell|PowerShell]] script. If you have another preferred language, you can easily translate it. | The following example uses [[library|WinSCP .NET assembly]] from a [[library_powershell|PowerShell]] script. If you have another preferred language, you can easily translate it. | ||
Line 7: | Line 8: | ||
<code powershell> | <code powershell> | ||
param ( | param ( | ||
- | $localPath = "c:\downloaded\", | + | $localPath = "c:\downloaded", |
- | $remotePath = "/home/user/" | + | $remotePath = "/home/user" |
) | ) | ||
Line 22: | Line 23: | ||
UserName = "user" | UserName = "user" | ||
Password = "mypassword" | Password = "mypassword" | ||
- | 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 50: | Line 51: | ||
# Download the selected file | # Download the selected file | ||
- | $session.GetFiles($session.EscapeFileMask($remotePath + $latest.Name), $localPath).Check() | + | $session.GetFileToDirectory($latest.FullName, $localPath) | Out-Null |
} | } | ||
finally | finally | ||
Line 60: | Line 61: | ||
exit 0 | exit 0 | ||
} | } | ||
- | catch [Exception] | + | catch |
{ | { | ||
- | Write-Host ("Error: {0}" -f $_.Exception.Message) | + | Write-Host "Error: $($_.Exception.Message)" |
exit 1 | exit 1 | ||
} | } | ||
Line 87: | Line 88: | ||
UserName = "user", | UserName = "user", | ||
Password = "mypassword", | Password = "mypassword", | ||
- | 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 95: | Line 96: | ||
session.Open(sessionOptions); | session.Open(sessionOptions); | ||
- | const string remotePath = "/home/user/"; | + | const string remotePath = "/home/user"; |
- | const string localPath = "c:\\downloaded\\"; | + | const string localPath = @"C:\downloaded"; |
// Get list of files in the directory | // Get list of files in the directory | ||
Line 115: | Line 116: | ||
// Download the selected file | // Download the selected file | ||
- | session.GetFiles(session.EscapeFileMask(remotePath + latest.Name), localPath).Check(); | + | session.GetFileToDirectory(latest.FullName, localPath); |
} | } | ||
Line 127: | Line 128: | ||
} | } | ||
} | } | ||
+ | </code> | ||
+ | |||
+ | ==== [[vbnet]] VB.NET ==== | ||
+ | |||
+ | The following snippet selects the most recent file from a directory listing. Most of the remaining code should be trivial to translate from the above C# example. | ||
+ | |||
+ | <code vbnet> | ||
+ | Dim latest As RemoteFileInfo = | ||
+ | directoryInfo.Files _ | ||
+ | .Where(Function(file) Not file.IsDirectory) _ | ||
+ | .OrderByDescending(Function(file) file.LastWriteTime) _ | ||
+ | .FirstOrDefault() | ||
</code> | </code> | ||
Line 140: | Line 153: | ||
Some of following alternatives can be easier to implement or actually even more appropriate for your specific task: | Some of following alternatives can be easier to implement or actually even more appropriate for your specific task: | ||
- | * Synchronizing a remote directory to a local directory (using ''[[scriptcommand_synchronize|synchronize local]]'' in scripting or ''[[library_session_synchronizedirectories|Session.SynchronizeDirectories(SynchronizationMode.Local, ...)]]'' in .NET assembly); | + | * Synchronizing a remote directory to a local directory (using ''[[scriptcommand_synchronize|synchronize local]]'' in scripting; or ''[[library_session_synchronizedirectories|Session.SynchronizeDirectories]]'' with ''mode'' parameter set to ''SynchronizationMode.Local'' in .NET assembly); |
* Downloading all files created in the last 24 hours (using [[file_mask|file mask]] ''*>=1D''; e.g. ''%%get -filemask="*>=1D" /home/user/*%%'', or an equivalent in .NET assembly). | * Downloading all files created in the last 24 hours (using [[file_mask|file mask]] ''*>=1D''; e.g. ''%%get -filemask="*>=1D" /home/user/*%%'', or an equivalent in .NET assembly). | ||
- | * Downloading all files created today (using ''[[scripting#timestamp|%TIMESTAMP%]]'' syntax to format [[file_mask|file mask]] with today's time constraint, e.g. ''%%get -filemask="*>=%TIMESTAMP#yyyy-mm-dd%" /home/user/*%%'', or an equivalent in .NET assembly). | + | * Downloading all files created today (using the [[file_mask#today|''today'' keyword in the file mask]]; e.g. ''%%get -filemask="*>=today" /home/user/*%%'', or an equivalent in .NET assembly). |
+ | * If you need to download all files that were not downloaded yet, but you do not keep local copies of the files to synchronize the remote directory against, see [[library_example_remember_downloaded_files|*]]. | ||
===== Further Reading ===== | ===== Further Reading ===== | ||
Line 148: | Line 162: | ||
* Guide to [[guide_automation|scripting/automation]]; | * Guide to [[guide_automation|scripting/automation]]; | ||
* [[library|WinSCP .NET assembly]]; | * [[library|WinSCP .NET assembly]]; | ||
- | * [[library_powershell|Using WinSCP .NET Assembly from PowerShell]]. | + | * [[library_powershell|*]]. |