This is an old revision of the document!
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
) to run Java Script or VB Script files.
Advertisement
- Windows command interpreter (batch files)
- Windows scripting host (Java or VB script)
- External script interpreters (PHP, Perl, etc)
- Further reading
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 (Java 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 grater 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 session\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); }
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
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 requried.
Now 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
Advertisement
Further reading
- Troubleshooting;
- Scripting documentation;
- Guide to automation;
- FAQ about scripting;
- Example scripts.