This guide contains simplified description of automating operations on FTP/SFTP server with WinSCP. You may want to see detailed documentation of the scripting functionality instead.
WinSCP offers scripting interface that you can use to automate many operations that it supports, including file transfers, synchronization and other operations.
There is also WinSCP .NET assembly build on top of the scripting interface. If you plan to call WinSCP from your .NET code, or if your task requires conditional processing, loops or other control structures, you should better use the .NET assembly. This guide focuses on simple automation tasks using scripting interface only.
To automate operation, you need to find out commands necessary to implement it. For simple operations you need at least to:
putcommand. For downloads use
getcommand. For synchronization use
synchronizecommand. For other operations, see supported commands.
For example a typical script to upload a file is:
# Connect to SFTP server using a password open sftp://user:firstname.lastname@example.org/ -hostkey="ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" # Upload file put d:\examplefile.txt /home/user/ # Exit WinSCP exit
You can have WinSCP generate a script template for you or even a complete batch file.
@echo off winscp.com /script=myscript.txt
Now to make using script easier/automatic you can:
.bat) or enter full command line to shortcut itself.1)
C:\Users\username\AppData\Roaming\Microsoft\Windows\SendToin Windows Vista and newer).
When connecting to SSH host, you will need to accept its host key.
When connecting to FTPS or WebDAVS host with certificate signed by untrusted authority you will need to verify the certificate.
You may want to modify the script automatically. For example you may want to operate it with different file each time.
For tasks involving more complex modifications, conditional processing, loops or other control structures, you should better use the WinSCP .NET assembly.
For simple modifications, you can pass the variable parts of the script from command line:
Execute the above script using syntax:
winscp.com /script=script.tmp /parameter // c:\myfile.txt
You can also use environment variables in the script.
Alternatively, you can generate new script file each time. To automate that, make a wrapper script file. For simple tasks you can use built-in Windows scripting functionality from batch file (
.bat). For complex tasks, you will need to use some scripting language, such JScript or VBScript from Windows script host or PHP or Perl.
Following example shows batch file that takes filename on command line and generates WinSCP script file to upload that file to remote server:
rem Generate temporary script to upload %1 echo open mysession >> script.tmp echo put %1 >> script.tmp echo exit >> script.tmp rem Execute script winscp.com /script=script.tmp rem Delete temporary script del script.tmp
Now you can run the batch file like (supposing you have saved it to file
See more hints on using parametrized batch file.
See guide to advanced scripting for examples of script generation using more powerful languages.
To check results of the script you can:
Once you find out what was the result of the script, you can perform any action you like. E.g. after evaluating exit code of WinSCP, you can send a “success” or “error” email. For that use any command-line email client you like, e.g. sendmail2).
You should also make the batch file indicate a result in its exit code, particularly if it is called from some parent system (for example SSIS).
See an example batch file:
winscp.com /script=example.txt if %ERRORLEVEL% neq 0 goto error echo Success sendmail.exe -t < success_mail.txt exit /b 0 :error echo Error! sendmail.exe -t < error_mail.txt exit /b 1
A similar error handling is used in the batch file template that WinSCP can generate for you.
Where for example content of
success_mail.txt may be:
From: email@example.com To: firstname.lastname@example.org Subject: Success The files were uploaded successfully.
If you require checking results of each command individually, you should better use the WinSCP .NET assembly. Alternatively, see guide to advanced scripting for examples of checking script results (including XML log parsing) using more powerful languages and guide to interpreting XML log for advanced scripting using C# language.
See example in scripting documentation.
.comfiles in general) directly from a shortcut. Call
winscp.comfrom a batch file or use
c:\usr\lib) directories in its installation instructions, as you will be running
sendmail.exedirectly from a Windows batch file. Just place sendmail files to any convenient location, e.g. along with WinSCP.
Site design by Black Gate