Differences
This shows you the differences between the selected revisions of the page.
| script_download_most_recent_file 2016-01-19 | 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 ==== | ||
| 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. | ||
| <code powershell> | <code powershell> | ||
| param ( | param ( | ||
| - | $localPath = "c:\downloaded\", | + | $localPath = "c:\downloaded", |
| - | $remotePath = "/home/user/" | + | $remotePath = "/home/user" |
| ) | ) | ||
| Line 17: | Line 18: | ||
| # Setup session options | # Setup session options | ||
| - | $sessionOptions = New-Object WinSCP.SessionOptions | + | $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ |
| - | ···$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp | + | ········Protocol = [WinSCP.Protocol]::Sftp |
| - | $sessionOptions.HostName = "example.com" | + | ·······HostName = "example.com" |
| - | $sessionOptions.UserName = "user" | + | ·······UserName = "user" |
| - | $sessionOptions.Password = "mypassword" | + | ·······Password = "mypassword" |
| - | $sessionOptions.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..." |
| + | } | ||
| $session = New-Object WinSCP.Session | $session = New-Object WinSCP.Session | ||
| Line 49: | 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 59: | Line 61: | ||
| exit 0 | exit 0 | ||
| } | } | ||
| - | catch [Exception] | + | catch |
| { | { | ||
| - | Write-Host $_.Exception.Message | + | Write-Host "Error: $($_.Exception.Message)" |
| exit 1 | exit 1 | ||
| } | } | ||
| </code> | </code> | ||
| - | ==== C# ==== | + | ==== [[csharp]] C# ==== |
| <code csharp> | <code csharp> | ||
| Line 86: | 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 94: | 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 114: | 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 126: | 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> | ||
| ===== [[scripting]] Using WinSCP Scripting ===== | ===== [[scripting]] Using WinSCP Scripting ===== | ||
| - | ==== In the Beta Version ==== | ||
| - | &beta | ||
| - | |||
| Use the ''[[scriptcommand_get#latest|-latest]]'' switch of the ''[[scriptcommand_get|get]]'' command: | Use the ''[[scriptcommand_get#latest|-latest]]'' switch of the ''[[scriptcommand_get|get]]'' command: | ||
| Line 138: | Line 149: | ||
| </code> | </code> | ||
| - | ==== In the Stable Version ==== | + | ===== [[alternatives]] Alternatives ===== |
| - | + | ||
| - | You may use following [[guide_automation_advanced#wsh|Windows script host JScript code]] (''example.js''): | + | |
| - | + | ||
| - | <code javascript> | + | |
| - | // Configuration | + | |
| - | + | ||
| - | // Local path to download to (keep trailing slash) | + | |
| - | var LOCALPATH = "c:\\downloaded\\"; | + | |
| - | // Remote path to search in (keep trailing slash) | + | |
| - | var REMOTEPATH = "/home/user/"; | + | |
| - | // Mask of files to search for | + | |
| - | var FILEMASK = "*.*"; | + | |
| - | // Session to connect to | + | |
| - | var SESSION = "session"; | + | |
| - | // Path to winscp.com | + | |
| - | var WINSCP = "c:\\program files\\winscp\\winscp.com"; | + | |
| - | + | ||
| - | var filesys = WScript.CreateObject("Scripting.FileSystemObject"); | + | |
| - | var shell = WScript.CreateObject("WScript.Shell"); | + | |
| - | + | ||
| - | var logfilepath = filesys.GetSpecialFolder(2) + "\\" + filesys.GetTempName() + ".xml"; | + | |
| - | + | ||
| - | var exec; | + | |
| - | + | ||
| - | // run winscp to get list of file in the remote directory into XML log | + | |
| - | exec = shell.Exec("\"" + WINSCP + "\" /xmllog=\"" + logfilepath + "\""); | + | |
| - | exec.StdIn.Write( | + | |
| - | "option batch abort\n" + | + | |
| - | "open \"" + SESSION + "\"\n" + | + | |
| - | "ls \"" + REMOTEPATH + FILEMASK + "\"\n" + | + | |
| - | "exit\n"); | + | |
| - | + | ||
| - | // wait until it finishes and collect its output | + | |
| - | var output = exec.StdOut.ReadAll(); | + | |
| - | // optionally print the output | + | |
| - | WScript.Echo(output); | + | |
| - | + | ||
| - | if (exec.ExitCode != 0) | + | |
| - | { | + | |
| - | WScript.Echo("Error retrieving list of files"); | + | |
| - | WScript.Quit(1); | + | |
| - | } | + | |
| - | + | ||
| - | // look for log file | + | |
| - | var logfile = filesys.GetFile(logfilepath); | + | |
| - | + | ||
| - | if (logfile == null) | + | |
| - | { | + | |
| - | WScript.Echo("Cannot find log file"); | + | |
| - | WScript.Quit(1); | + | |
| - | } | + | |
| - | + | ||
| - | // parse XML log file | + | |
| - | var doc = new ActiveXObject("MSXML2.DOMDocument"); | + | |
| - | doc.async = false; | + | |
| - | doc.load(logfilepath); | + | |
| - | + | ||
| - | doc.setProperty("SelectionNamespaces", | + | |
| - | "xmlns:w='http://winscp.net/schema/session/1.0'"); | + | |
| - | + | ||
| - | var nodes = doc.selectNodes("//w:file"); | + | |
| - | + | ||
| - | // find the latest file | + | |
| - | var filenameLatest = null; | + | |
| - | var modificationLatest = null; | + | |
| - | for (var i = 0; i < nodes.length; ++i) | + | |
| - | { | + | |
| - | var filename = nodes[i].selectSingleNode("w:filename/@value"); | + | |
| - | var modification = nodes[i].selectSingleNode("w:modification/@value"); | + | |
| - | if ((filename != null) && | + | |
| - | (filename.value != ".") && | + | |
| - | (filename.value != "..") && | + | |
| - | (modification != null)) | + | |
| - | { | + | |
| - | // can compare timestamps stringwise | + | |
| - | if ((modificationLatest == null) || | + | |
| - | (modificationLatest < modification.value)) | + | |
| - | { | + | |
| - | modificationLatest = modification.value; | + | |
| - | filenameLatest = filename.value; | + | |
| - | } | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | // no file in the log | + | |
| - | if (filenameLatest == null) | + | |
| - | { | + | |
| - | WScript.Echo("No file found"); | + | |
| - | WScript.Quit(0); | + | |
| - | } | + | |
| - | + | ||
| - | // run winscp to download the latest file | + | |
| - | exec = shell.Exec("\"" + WINSCP + "\""); | + | |
| - | exec.StdIn.Write( | + | |
| - | "option batch abort\n" + | + | |
| - | "option confirm off\n" + | + | |
| - | "open \"" + SESSION + "\"\n" + | + | |
| - | "get \"" + REMOTEPATH + filenameLatest + "\" \"" + LOCALPATH + "\"\n" + | + | |
| - | "exit\n"); | + | |
| - | + | ||
| - | // wait until it finishes and collect its output | + | |
| - | var output = exec.StdOut.ReadAll(); | + | |
| - | // optionally print the output | + | |
| - | WScript.Echo(output); | + | |
| - | + | ||
| - | if (exec.ExitCode != 0) | + | |
| - | { | + | |
| - | WScript.Echo("Error downloading " + filenameLatest); | + | |
| - | WScript.Quit(1); | + | |
| - | } | + | |
| - | </code> | + | |
| - | + | ||
| - | Run the script with command: | + | |
| - | <code batch> | + | |
| - | cscript /nologo example.js | + | |
| - | </code> | + | |
| - | + | ||
| - | ===== Alternatives ===== | + | |
| 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 268: | 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|*]]. |