This is an old revision of the document!

Documentation » 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.

Advertisement

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()
}
else
{
    Write-Host ("File {0} does not exist" -f $remotePath)
}

See also:

Advertisement

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 winscp.com /script=upload.txt

See also:

Last modified: by martin