Documentation » Using WinSCP » Guides » Scripting/Automation »

Advanced FTP/SFTP scripting

WinSCP .NET assembly mostly deprecates techniques demostrated in this article. Using the assembly is now preferred approach for advanced automation tasks with WinSCP.

Before reading this guide make sure you are familiar with WinSCP scripting. For that you may want to read guide to automation.

Scripting functionality of WinSCP does not support any control sequences, manipulation of file paths, etc. If you need these, you have to call WinSCP script from wrapper script implemented in another scripting language.


Choosing Language

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 (batch files)

Windows command interpreter executes .bat or .cmd files. Some of the features it supports:

  • Conditional execution based on an exit code of an application or a value of an environment variable;
  • An iteration over files matching a mask;
  • Setting and querying environment variables;
  • Simple file path manipulation (extracting a directory from a path, extracting an extension, etc.).


See guide to automation for some examples.

Windows script host (JScript or VB script)

Windows script host is an automation technology for Microsoft Windows that provides scripting capabilities comparable to batch files, but with a greater range of supported features. It is language-independent. By default it interprets and runs plain-text JScript (JavaScript-like) and VBScript. Users can install different scripting engines.

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):

cscript /nologo example.js

Access to Input/Output streams

You can use WScript.Shell to execute WinSCP console interface tool and feed the script commands (using input stream), without creating temporary script file:

var shell = WScript.CreateObject("WScript.Shell");
// run (make it log to XML)
var exec = shell.Exec(" /xmllog=log.xml");
// feed the commands
    "option batch abort\n" +
    "open mysession\n" +
    "ls\n" +
// wait until it finishes and collect its output
var output = exec.StdOut.ReadAll();
// optionally print the output

XML parsing

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):

// parse XML log file
var doc = new ActiveXObject("MSXML2.DOMDocument");
doc.async = false;
// look for file tags
var nodes = doc.selectNodes("//w:file");
WScript.Echo("There are " + nodes.length + " files and subdirectories:");
// list the files
for (var i = 0; i < nodes.length; ++i)
    var filename = nodes[i].selectSingleNode("w:filename/@value").value;


Running the script will produce output like:

There are 14 files and subdirectories:

See also the guide Interpreting XML log for advanced scripting (uses C# language, though could be easily rewritten to JScript or VB script).

Hidden execution

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.

Some notes for using wscript:

  • Every call to WScript.Echo causes message box to be shown, pausing the execution. So generaly you do not want to use WScript.Echo from wscript.exe (except maybe to report errors).
  • Execution of opens new console window (if one is not opened already, e.g. by cscript.exe). Use winscp.exe instead (without /console switch). However winscp.exe does not allow input/output redirection, so you need to pass the commands in using command-line.

.NET (C# or VB.NET)

See SFTP file transfers in .NET.

External script interpreters (PowerShell, PHP, Perl, etc)

If you are familiar with other scripting languages, you can use those.

All versions of Windows since Windows XP have PowerShell available. With PowerShell, it is more convenient to use WinSCP .NET assembly, than the simple scripting. There are lot of examples of use of the WinSCP .NET assembly in PowerShell.

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(" /script=example.txt", $exitcode);
if ($exitcode == 0)
    echo "success\n";
    echo "error\n";
    // handle an error

with simple command:

php.exe example.php

Further reading

Last modified: by martin