Differences

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

2013-05-20 2013-05-21
library_powershell to trailer (martin) powershell example (martin)
Line 142: Line 142:
===== [[download_most_recent_file]] Downloading the most recent file ===== ===== [[download_most_recent_file]] Downloading the most recent file =====
-&deprecated_use_net 
 +==== Using WinSCP .NET Assembly ====
 +Use [[library|WinSCP .NET assembly]] from your favourite language. Use relevant construct of your language or API of your runtime environment for the file name formatting.
 +
 +If you do not have your favourite language, use [[library_powershell|PowerShell]]:
 +
 +&beta (* .dll *)
 +<code powershell>
 +try
 +{
 +    # Load WinSCP .NET assembly
 +    # Use "winscp.dll" for the releases before the latest beta version.
 +    [Reflection.Assembly]::LoadFrom("WinSCPnet.dll") | Out-Null
 +
 +    # Setup session options
 +    $sessionOptions = New-Object WinSCP.SessionOptions
 +    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
 +    $sessionOptions.HostName = "example.com"
 +    $sessionOptions.UserName = "user"
 +    $sessionOptions.Password = "mypassword"
 +    $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
 +
 +    $session = New-Object WinSCP.Session
 +
 +    try
 +    {
 +        # Connect
 +        $session.Open($sessionOptions)
 +
 +        $localPath = "c:\downloaded\"
 +        $remotePath = "/home/user/"
 +
 +        # Gel list of files in the directory
 +        $files = $session.ListDirectory($remotePath)
 +
 +        # Select the most recent file
 +        $latest =
 +            $files.Files |
 +            Where-Object { -not $_.IsDirectory } |
 +            Sort-Object LastWriteTime |
 +            Select-Object -Last 1
 +
 +        # Any file at all?
 +        if ($latest -eq $Null)
 +        {
 +            Write-Host "No file found"
 +            exit 1
 +        }
 +
 +        # Download the selected file
 +        $session.GetFiles(($remotePath + $latest.Name), $localPath).Check()
 +    }
 +    finally
 +    {
 +        # Disconnect, clean up
 +        $session.Dispose()
 +    }
 +
 +    exit 0
 +}
 +catch [Exception]
 +{
 +    Write-Host $_.Exception.Message
 +    exit 1
 +}
 +</code>
 +
 +==== Using WinSCP Scripting ====
You may use following [[guide_automation_advanced#wsh|Windows script host JavaScript code]] (''example.js''): You may use following [[guide_automation_advanced#wsh|Windows script host JavaScript code]] (''example.js''):

Last modified: by martin