This is an old revision of the document!

Documentation » Using WinSCP » Guides » Scripting/Automation »

Advanced FTP/SFTP scripting

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.

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.

Advertisement

Windows command interpreter (batch files)

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

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

See guide to automation for some examples.

Windows scripting 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.

Advertisement

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 JavaScript 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("winscp.com /log=log.xml");
// feed the commands
exec.StdIn.Write(
    "option batch abort\n" +
    "open mysession\n" +
    "ls\n" +
    "exit\n");
// wait until it finishes and collect its output
var output = exec.StdOut.ReadAll();
// optionally print the output
WScript.Echo(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;
doc.load("log.xml");
 
doc.setProperty("SelectionNamespaces", 
    "xmlns:w='http://winscp.net/schema/session/1.0'");
 
// 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;
    WScript.Echo(filename);
}

Advertisement

Running the script will produce output like:

There are 14 files and subdirectories:
.
..
.htaccess
commandline.txt
config.txt
directory_cache.txt
dragext.txt
faq.txt
faq_commandline.txt
faq_dir_default.txt
faq_download_temp.txt
faq_drag_move.txt
faq_error_code.txt
faq_filemanager.txt

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

Hidden execution

When executing JavaScript code with cscript.exe, console window is shown. If you want to execute the JavaScript 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 winscp.com 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 guide to using WinSCP from .NET.

External script interpreters (PHP, Perl, etc)

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

Note that while you may find installing new scripting engine to Windows troublesome, 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 php5ts.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
}
?>

Advertisement

with simple command:

php.exe example.php

Further reading

Last modified: by martin