Contents » Using WinSCP » Guides » Scripting/Automation »

Conditional Processing in Automation

When automating an operation with WinSCP, you may want to perform it only when a certain condition is met. Typically you may want to test if a particular file exists before attempting to transfer it.

Using WinSCP .NET Assembly

Particularly for complex conditions or conditions involving remote files, using WinSCP .NET assembly is the easiest and the recommended approach.


An example of PowerShell script that checks if a remote file exists before downloading it:

$remotePath = "/home/user/test.txt"
$localPath = "C:\download\test.txt"
if ($session.FileExists($remotePath))
    Write-Host ("File {0} exists, downloading" -f $remotePath)
    $session.GetFiles($remotePath, $localPath).Check()
    Write-Host ("File {0} does not exist" -f $remotePath)

See also:

Conditions in a Batch File

If you do not want to use .NET assembly, for simple conditions or conditions involving local files, you may be able to move the condition outside of WinSCP script into a wrapper batch file.

For example you can use an if command to test a local file existence before uploading it:

if exist c:\upload\test.txt /script=upload.txt

See also:

  guide_automation_conditional.txt · Last modified: by martin

Search Documentation

This page


About donations

$9   $19   $49   $99

About donations



Site design by Black Gate