Differences

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

script_local_move_after_successful_upload 2014-12-22 script_local_move_after_successful_upload 2023-06-05 (current)
Line 2: Line 2:
===== [[library]] Using WinSCP .NET Assembly ===== ===== [[library]] Using WinSCP .NET Assembly =====
-Use [[library|WinSCP .NET assembly]] from your favorite language.  +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.
- +
-If you do not have your favorite language, use [[library_powershell|PowerShell]]:+
<code powershell> <code powershell>
Line 19: Line 17:
    # 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 42: Line 41:
            if ($transfer.Error -eq $Null)             if ($transfer.Error -eq $Null)
            {             {
-                Write-Host ("Upload of {0} succeeded, moving to backup" -f +                Write-Host "Upload of $($transfer.FileName) succeeded, moving to backup"
-                    $transfer.FileName)+
                # Upload succeeded, move source file to backup                 # Upload succeeded, move source file to backup
                Move-Item $transfer.FileName $backupPath                 Move-Item $transfer.FileName $backupPath
Line 49: Line 47:
            else             else
            {             {
-                Write-Host ("Upload of {0} failed: {1}" -+                Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)"
- ···················$transfer.FileName, $transfer.Error.Message)+
            }             }
        }         }
Line 62: Line 59:
    exit 0     exit 0
} }
-catch [Exception]+catch
{ {
-    Write-Host $_.Exception.Message+    Write-Host "Error: $($_.Exception.Message)"
    exit 1     exit 1
} }
</code> </code>
-===== Using WinSCP Scripting =====+===== [[scripting]] Using WinSCP Scripting =====
WinSCP scripting does not support move command for local files. Instead you can combine WinSCP script with batch file: WinSCP scripting does not support move command for local files. Instead you can combine WinSCP script with batch file:
<code winscp> <code winscp>
-option batch abort 
-option confirm off 
# Connect # Connect
open mysession open mysession
# Upload the files # Upload the files
put *.* put *.*
 +# Exit WinSCP
 +exit
</code> </code>
Line 89: Line 86:
echo Upload succeeded, moving local files echo Upload succeeded, moving local files
move *.* c:\backup\ move *.* c:\backup\
-exit 0+exit /b 0
:error :error
echo Upload failed, keeping local files echo Upload failed, keeping local files
-exit 1+exit /b 1
</code> </code>
 +
 +Contrary to the PowerShell script above, this solution is not transactionally safe. If new files appear in the local folder between times the upload starts and the archiving/moving starts, the new files will not get uploaded. For this reason, prefer using the PowerShell script.
 +
 +Another option is to move the files to an intermediate/temporary folder first, upload from there, and then move them to the backup location.
===== Further Reading ===== ===== Further Reading =====

Last modified: by martin