Differences
This shows you the differences between the selected revisions of the page.
script_download_most_recent_file 2019-07-02 | 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 xxxxxxxxxxx...=" | + | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
} | } | ||
Line 50: | Line 51: | ||
# Download the selected file | # Download the selected file | ||
- | $session.GetFiles( | + | $session.GetFileToDirectory($latest.FullName, $localPath) | Out-Null |
- | [WinSCP.RemotePath]::EscapeFileMask($latest.FullName), $localPath).Check() | + | |
} | } | ||
finally | finally | ||
Line 88: | Line 88: | ||
UserName = "user", | UserName = "user", | ||
Password = "mypassword", | Password = "mypassword", | ||
- | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...=", | + | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...", |
}; | }; | ||
Line 96: | 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 116: | Line 116: | ||
// Download the selected file | // Download the selected file | ||
- | session.GetFiles( | + | session.GetFileToDirectory(latest.FullName, localPath); |
- | RemotePath.EscapeFileMask(latest.FullName), localPath).Check(); | + | |
} | } | ||
Line 129: | 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 144: | Line 155: | ||
* 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); | * 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 [[file_mask|file mask]] ''*>=today''; e.g. ''%%get -filemask="*>=today" /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 150: | 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|*]]. |