Contents » Using WinSCP » Guides »

Advanced FTP/SFTP scripting

Table of Contents

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

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

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.

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
while (exec.Status == 0)
{
     WScript.Sleep(100);
}

If you want to see WinSCP output replace the trailing while statement with:

// wait until the script finishes
while (!exec.StdOut.AtEndOfStream)
{
    WScript.Echo(exec.StdOut.ReadAll());
}

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);
}

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

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:

.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
}
?>

with simple command:

php.exe example.php

Further reading

 
  guide_automation_advanced.txt · Last modified: 8 Jul 2009 by 78.106.160.43
 

Search

This page

Donate

Donate via PayPal

Associations

Site design by Black Gate