Differences
This shows you the differences between the selected revisions of the page.
| 2013-05-21 | 2013-05-21 | ||
| powershell example (martin) | powershell (martin) | ||
| Line 444: | Line 444: | ||
| ===== [[local_move_after_successful_upload]] Moving local files to different location after successful upload ===== | ===== [[local_move_after_successful_upload]] Moving local files to different location after successful upload ===== | ||
| - | &deprecated_use_net | ||
| - | WinSCP does not support move command for local files. Instead you can combine WinSCP script with batch file: | + | ==== Using WinSCP .NET Assembly ==== |
| + | Use [[library|WinSCP .NET assembly]] from your favourite language. | ||
| + | |||
| + | 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:\upload\*" | ||
| + | $remotePath = "/home/user/" | ||
| + | $backupPath = "C:\backup\" | ||
| + | |||
| + | # Upload files, collect results | ||
| + | $transferResult = $session.PutFiles($localPath, $remotePath) | ||
| + | |||
| + | # Iterate over every transfer | ||
| + | foreach ($transfer in $transferResult.Transfers) | ||
| + | { | ||
| + | # Success or error? | ||
| + | if ($transfer.Error -eq $Null) | ||
| + | { | ||
| + | Write-Host ("Upload of {0} succeeded, moving to backup" -f | ||
| + | $transfer.FileName) | ||
| + | # Upload succeeded, move source file to backup | ||
| + | Move-Item $transfer.FileName $backupPath | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | Write-Host ("Upload of {0} failed: {1}" -f | ||
| + | $transfer.FileName, $transfer.Error.Message) | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | finally | ||
| + | { | ||
| + | # Disconnect, clean up | ||
| + | $session.Dispose() | ||
| + | } | ||
| + | |||
| + | exit 0 | ||
| + | } | ||
| + | catch [Exception] | ||
| + | { | ||
| + | Write-Host $_.Exception.Message | ||
| + | exit 1 | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | ==== Using WinSCP Scripting ==== | ||
| + | WinSCP scripting does not support move command for local files. Instead you can combine WinSCP script with batch file: | ||
| <code winscp> | <code winscp> | ||