Differences

This shows you the differences between the selected revisions of the page.

script_download_most_recent_file 2015-12-21 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 ====
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 16: 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 48: 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 58: Line 61:
    exit 0     exit 0
} }
-catch [Exception]+catch
{ {
-    Write-Host $_.Exception.Message+    Write-Host "Error: $($_.Exception.Message)"
    exit 1     exit 1
} }
</code> </code>
-===== [[scripting]] Using WinSCP Scripting ===== +==== [[csharp]] C# ====
-==== In the Beta Version ==== +
-&beta+
-Use the ''-latest'' switch of the ''[[scriptcommand_get|get]]'' command:+<code csharp&gt; 
 +using System; 
 +using System.Linq; 
 +using WinSCP;
-&lt;code winscp&gt+class Program 
-get -latest /home/user/* c:\downloaded\ +
-&lt;/code&gt;+    static int Main(string[] args) 
 +    { 
 +        try 
 +        { 
 +            // Setup session options 
 +            SessionOptions sessionOptions = new SessionOptions 
 +            { 
 +                Protocol = Protocol.Sftp, 
 +                HostName = &quot;example.com&quot;, 
 + ···············UserName = &quot;user&quot;, 
 + ···············Password = &quot;mypassword", 
 +················SshHostKeyFingerprint = &quot;ssh-rsa 2048 xxxxxxxxxxx...&quot;, 
 +            };
-==== In the Stable Version ====+············using (Session session = new Session()) 
 +           { 
 +               // Connect 
 +················session.Open(sessionOptions);
-You may use following [[guide_automation_advanced#wsh|Windows script host JScript code]] (''example.js''):+                const string remotePath = &quot;/home/user"; 
 +               const string localPath = @&quot;C:\downloaded&quot;;
-&lt;code javascript&gt; +················// Get list of files in the directory 
-// Configuration+ ···············RemoteDirectoryInfo directoryInfo = session.ListDirectory(remotePath);
-// Local path to download to (keep trailing slash) +················// Select the most recent file 
-var LOCALPATH = &quot;c:\\downloaded\\"; + ···············RemoteFileInfo latest = 
-// Remote path to search in (keep trailing slash) + ···················directoryInfo.Files 
-var REMOTEPATH = "/home/user/&quot;; + ·······················.Where(file =&gt; !file.IsDirectory) 
-// Mask of files to search for + ·······················.OrderByDescending(file =&gt; file.LastWriteTime) 
-var FILEMASK = "*.*&quot;; + ·······················.FirstOrDefault();
-// Session to connect to +
-var SESSION = "session&quot;+
-// Path to winscp.com +
-var WINSCP = "c:\\program files\\winscp\\winscp.com&quot;;+
-var filesys = WScript.CreateObject(&quot;Scripting.FileSystemObject"); +                // Any file at all? 
-var shell = WScript.CreateObject("WScript.Shell"); +················if (latest == null
- ·· + ···············{ 
-var logfilepath = filesys.GetSpecialFolder(2) + "\\" + filesys.GetTempName() + ";.xml";+ ···················throw new Exception("No file found"); 
 +················}
-var exec;+                // Download the selected file 
 +                session.GetFileToDirectory(latest.FullName, localPath); 
 +            }
-// run winscp to get list of file in the remote directory into XML log +············return 0; 
-exec = shell.Exec("\"" + WINSCP + "\" /xmllog=\"" + logfilepath + "\""); + ·······
-exec.StdIn.Write( +        catch (Exception e)
-    "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; +            Console.WriteLine(";Error: {0}", e)
- ···········filenameLatest = filename.value;+            return 1;
        }         }
    }     }
} }
 +</code>
-// no file in the log +==== [[vbnet]] VB.NET ====
-if (filenameLatest == null) +
-{ +
-   WScript.Echo("No file found&quot;); +
-    WScript.Quit(0); +
-}+
-// run winscp to download the latest file +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.
-exec = shell.Exec("\"" + WINSCP + "\""); +
-exec.StdIn.Write( +
-   "option batch abort\n" +
-   "option confirm off\n" +
-   "open \"" + SESSION + &quot;\"\n" + +
-   "get \"" + REMOTEPATH + filenameLatest + "\" \"" + LOCALPATH + "\"\n" + +
-    "exit\n");+
-// wait until it finishes and collect its output +&lt;code vbnet&gt
-var output = exec.StdOut.ReadAll()+Dim latest As RemoteFileInfo = 
-// optionally print the output +····directoryInfo.Files _ 
-WScript.Echo(output); +········.Where(Function(file) Not file.IsDirectory) _ 
- + ·······.OrderByDescending(Function(file) file.LastWriteTime) _ 
-if (exec.ExitCode != 0) + ·······.FirstOrDefault()
-{ +
-   WScript.Echo(&quot;Error downloading &quot; + filenameLatest); +
-   WScript.Quit(1)+
-}+
</code> </code>
-Run the script with command: +===== [[scripting]] Using WinSCP Scripting ===== 
-<code batch+Use the ''[[scriptcommand_get#latest|-latest]]'' switch of the ''[[scriptcommand_get|get]]'' command: 
-cscript /nologo example.js+ 
 +<code winscp
 +get -latest /home/user/* c:\downloaded\
</code> </code>
-===== Alternatives =====+===== [[alternatives]] 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 205: 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|*]].

Last modified: by martin