This is an old revision of the document!

Documentation » Using WinSCP » Guides » Scripting/Automation »

== Automate file transfers (or synchronization) to FTP server or SFTP server ==

Advertisement

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.

Before Starting

Before starting you should:

secure http

= Commands =To automate operation, you need to find out commands necessary to implement it. For simple operations you need at least to: * Set script to be performed non-interactively. For most automation scripts, you should use commands option batch abort and option confirm off. * Open session using open command. * Perform operation. For uploads use put command. For downloads use get command. For synchronization use synchronize command. For other operations, see supported commands. * Exit scripting using exit command.For example a typical script to upload a file is:

# Automatically abort script on errorsoption batch abort# Disable overwrite confirmations that conflict with the previousoption confirm off# Connect to SFTP server using a passwordopen sftp://user:password@example.com/ -hostkey="ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"# Upload fileput d:\examplefile.txt /home/user# Disconnectclose 
 

= Script file =Assemble the commands into a script file. You can name the script file as you like. See simple example and some useful scripts.Use the /script command line option to pass the script to the WinSCP executable. You can embed the complete command line into a Windows batch file (.bat), like as follows:

@echo offwinscp.com /script=myscript.txt

=

using Using script =Now to make using script easier/automatic you can: * Make shortcut to it on desktop to ease execution. Either make shortcut to batch file (.bat) or enter full command line to shortcut itself.1 * If the wrapping batch file takes filename as command line parameter (see below) you can: * Make shortcut to it on desktop and use it by dropping files on the icon. Windows automatically run the batch file and passes path to dropped file as command-line parameter. * In a similar way you can put the shortcut to the batch file into Explorer’s ‘Send To’ context menu (C:\Users\username\AppData\Roaming\Microsoft\Windows\SendTo in Windows Vista and newer). * Schedule automatic execution.= Notes =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.= parametrized Modifying the script automatically =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:

option batch abortoption confirm offopen mysessionput %1%exit

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 %1echo option batch abort > script.tmpecho option confirm off >> script.tmpecho open mysession >> script.tmpecho put %1 >> script.tmpecho exit >> script.tmprem Execute scriptwinscp.com /script=script.tmprem Delete temporary scriptdel script.tmp 
 

Now you can run the batch file like (supposing you have saved it to file upload.bat):

upload.bat c:\myfile.txt

= See more hints on using parametrized batch file.See guide to advanced scripting for examples of script generation using more powerful languages.

Advertisement

results Checking script results =To check results of the script you can: * Check exit code of WinSCP (exit code is the only relevant and reliable way to check if script completed successfully). See example below and FAQ. * Save and inspect log file. XML log format is recommended. Use command-line parameter /xmllog. * Save and inspect output of the script. Use output redirection.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.txtif %ERRORLEVEL% neq 0 goto errorecho Successsendmail.exe -t < success_mail.txtexit 0:errorecho Error!sendmail.exe -t < error_mail.txtexit 1

Where for example content of success_mail.txt may be:

From: script@example.comTo: me@example.comSubject: SuccessThe files were uploaded successfully.

= Example 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.= Further reading = * Troubleshooting; * Scripting documentation; * Guide to advanced scripting; * WinSCP .NET assembly; * Command-line parameters; * WinSCP executables; * FAQ about scripting; * Example scripts; * Schedule file transfers or synchronization.

  1. Note that it is not possible to use winscp.com (.com files in general) directly from a shortcut. Call winscp.com from a batch file or use winscp.exe with /console command-line parameter.Back
  2. When installing sendmail, you can ignore all references to /usr/lib/ (or c:\usr\lib) directories in its installation instructions, as you will be running sendmail.exe directly from a Windows batch file. Just place sendmail files to any convenient location, e.g. along with WinSCP.Back

Last modified: by 154.122.119.149