Advanced FTP - SFTP Scripting - WinSCP
Advanced FTP - SFTP Scripting - WinSCP
Before reading this guide make sure you are familiar with WinSCP scripting. For
that you may want to read guide to automation.
Choosing Language
Windows command interpreter (batch files)
Windows script host (JScript or VB script)
Access to Input/Output streams
XML parsing
Hidden execution
Use language of your preference. If you have no preference or do not know any
scripting language, the easiest may be to use scripting engines available in
Windows:
Windows command interpreter (cmd.exe) to run .bat or .cmd files (for simple
tasks);
Windows script host (cscript.exe or wscript.exe) to run Java Script or VB
Script files.
Windows command interpreter executes .bat or .cmd files. Some of the features it
supports:
It supports all the features listed in Windows command interpreter section above
(with greater flexibility). Plus it includes many advanced functions, you may find
useful when using together with WinSCP. See sections below.
To use the examples below copy the JScript code to file (e.g. example.js) and use
cscript.exe to execute it (use /nologo to suppress banner):
You can use WScript.Shell to execute WinSCP console interface tool and feed the
script commands (using input stream), without creating temporary script file:
You can use MSXML2.DOMDocument object to parse XML log produced by WinSCP. The
following example follows up to the previous one (which starts session with XML
logging and lists contents of initial directory):
doc.setProperty("SelectionNamespaces",
"xmlns:w='http://winscp.net/schema/session/1.0'");
See also the guide Interpreting XML log for advanced scripting (uses C# language,
though could be easily rewritten to JScript or VB script).
When executing JScript code with cscript.exe, console window is shown. If you
want to execute the JScript without showing console window, use wscript.exe
instead.
If you are familiar with other scripting languages, you can use those.
If you want to use your another favorite scripting language, but you do not have it
on your Windows machine, you may find installing it troublesome. But you may not
need to install anything actually. E.g. for using PHP interpreter, you can just grab
the Windows binary PHP zip package and extract php.exe and php7ts.dll files out
of it. These two binaries alone support most (if not all) features you need. No
installing or registration is required.
Having the two binaries you can execute PHP script example.php:
<?
system("winscp.com /script=example.txt", $exitcode);
if ($exitcode == 0)
{
echo "success\n";
}
else
{
echo "error\n";
// handle an error
}
?>
php.exe example.php
Troubleshooting;
WinSCP .NET assembly;
Scripting documentation;
Guide to automation;
FAQ about scripting;
Example scripts.