Differences
This shows you the differences between the selected revisions of the page.
2013-05-20 | 2013-05-20 | ||
streamlining jscript examples (martin) | powershell example (martin) | ||
Line 28: | Line 28: | ||
===== [[download_timestamped_filename]] Downloading file to timestamped-filename ===== | ===== [[download_timestamped_filename]] Downloading file to timestamped-filename ===== | ||
- | &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/" | ||
+ | $file = "download.txt" | ||
+ | |||
+ | # Format timestamp | ||
+ | $stamp = $(Get-Date -f "yyyyMMddHHmmss") | ||
+ | |||
+ | # Download the file and throw on any error | ||
+ | $session.GetFiles( | ||
+ | ($remotePath + $file), | ||
+ | ($localPath + $file + "." + $stamp)).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''): | ||