Professional Documents
Culture Documents
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 built on top of the scripting interface. If you
plan to call WinSCP from your .NET code or PowerShell, 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
Commands
Script file
Generating script
Using script
Notes
Modifying the script automatically
Checking script results
Example
Further reading
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.
Generally, you should also use /ini=nul switch to isolate the script execution from
GUI configuration and /log= switch to enable session logging. You can embed the
complete command line into a Windows batch file (.bat), like as follows:
@echo off
winscp.com /ini=nul /log=myscript.log /script=myscript.txt
You can have WinSCP generate a script template for you or even a complete batch
file.
When connecting to the SSH host, you will need to accept its host key.
You may want to modify the script automatically. For example you may want to
operate it with different file each time.
For simple modifications, you can pass the variable parts of the script from
command line:
open mysession
put %1%
exit
Execute the above script using syntax:
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:
Now you can run the batch file like (supposing you have saved it to file upload.bat):
upload.bat c:\myfile.txt
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: print a message, send an email, etc.
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).
A similar error handling is used in the batch file template that WinSCP can generate
for you.
If you require checking results of each command individually, you should better use
the WinSCP .NET assembly. Alternatively, see the guide Advanced FTP/SFTP
scripting for examples of checking script results (including XML log parsing) using
more powerful languages and the guide to Interpreting XML log for advanced
scripting using C# language.
Troubleshooting;
Scripting documentation;
Guide to Advanced FTP/SFTP scripting;
WinSCP .NET assembly and COM library;
Command-line parameters;
WinSCP executables;
FAQ about scripting;
Example scripts;
Schedule file transfers (or synchronization) to FTP/SFTP server.
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