Differences

This shows you the differences between the selected revisions of the page.

guide_automation_advanced 2009-04-14 guide_automation_advanced 2022-10-21 (current)
Line 1: Line 1:
-====== Advanced SFTP/FTP scripting ======+====== Advanced FTP/SFTP scripting ====== 
 +&deprecated_use_net 
//Before reading this guide make sure you are familiar with WinSCP [[scripting]]. For that you may want to read [[guide_automation|guide to automation]]//. //Before reading this guide make sure you are familiar with WinSCP [[scripting]]. For that you may want to read [[guide_automation|guide to automation]]//.
[[scripting|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. [[scripting|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: 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 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.+  * Windows script host (''[[https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cscript|cscript.exe]]'' or ''[[https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/wscript|wscript.exe]]'') to run Java Script or VB Script files.
-===== Windows command interpreter (batch files) =====+===== [[batch_file]] Windows command interpreter (batch files) =====
Windows command interpreter executes ''.bat'' or ''.cmd'' files. Some of the features it supports: 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; +  * Conditional execution based on an exit code of an application or a value of an environment variable; 
-  * Iteration over files matching mask;+  * An iteration over files matching a mask;
  * Setting and querying environment variables;   * Setting and querying environment variables;
-  * Simple file path manipulation (extracting directory from path, extracting extension, etc.).+  * Simple file path manipulation (extracting a directory from a path, extracting an extension, etc.).
See [[guide_automation|guide to automation]] for some examples. See [[guide_automation|guide to automation]] for some examples.
-===== Windows scripting host (Java or VB script) ===== +===== [[wsh]] Windows script host (JScript or VB script) ===== 
-[[wp>Windows Script Host|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.+[[wp>Windows_Script_Host|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 grater flexibility). Plus it includes many advanced functions, you may find useful when using together with WinSCP. See sections below.+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): +To use the examples below copy the JScript code to file (e.g. ''example.js'') and use ''[[https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cscript|cscript.exe]]'' to execute it (use ''/nologo'' to suppress banner): 
-<code>+<code batch>
cscript /nologo example.js cscript /nologo example.js
</code> </code>
-==== Access to Input/Output streams ==== +==== [[inout]] Access to Input/Output streams ==== 
-You can use ''[[http://msdn.microsoft.com/en-us/library/6kxy1a51(VS.85).aspx|Scripting.FileSystemObject]]'' to execute WinSCP console interface tool and feed the script commands (using input stream), without creating temporary script file:+You can use ''[[https://learn.microsoft.com/en-us/previous-versions/aew9yb99(v=vs.85)|WScript.Shell]]'' to execute WinSCP [[executables|console interface tool]] and feed the script commands (using input stream), without creating temporary script file:
<code javascript> <code javascript>
var shell = WScript.CreateObject("WScript.Shell"); var shell = WScript.CreateObject("WScript.Shell");
// run (make it log to XML) // run (make it log to XML)
-var exec = shell.Exec("winscp.com /log=log.xml");+var exec = shell.Exec("winscp.com /xmllog=log.xml");
// feed the commands // feed the commands
exec.StdIn.Write( exec.StdIn.Write(
    "option batch abort\n" +     "option batch abort\n" +
-    "open session\n" ++    "open mysession\n" +
    "ls\n" +     "ls\n" +
    "exit\n");     "exit\n");
-// wait until it finishes +// wait until it finishes and collect its output 
-while (exec.Status == 0+var output = exec.StdOut.ReadAll(); 
-{ +// optionally print the output 
- ····WScript.Sleep(100); +WScript.Echo(output);
-}+
</code> </code>
-==== XML parsing ==== +==== [[wsh_xml_parsing]] XML parsing ==== 
-You can use ''[[http://msdn.microsoft.com/en-us/library/ms756987(VS.85).aspx|MSXML2.DOMDocument]]'' object to parse [[logging_xml|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):+You can use ''[[https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms756987(v=vs.85)|MSXML2.DOMDocument]]'' object to parse [[logging_xml|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):
<code javascript> <code javascript>
// parse XML log file // parse XML log file
Line 89: Line 92:
</code> </code>
-===== External script interpreters (PHP, Perl, etc) =====+//See also the guide [[guide_interpreting_xml_log|*]] (uses C# language, though could be easily rewritten to JScript or VB script).// 
 + 
 +==== Hidden execution ==== 
 +When executing JScript code with ''[[https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cscript|cscript.exe]]'', console window is shown. If you want to execute the JScript without showing console window, use ''[[https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/wscript|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 ''[[executables|winscp.com]]'' opens new console window (if one is not opened already, e.g. by ''cscript.exe''). Use ''[[executables|winscp.exe]]'' instead (without ''[[commandline#scripting|/console]]'' switch). However ''winscp.exe'' does not allow input/output redirection, so you need to pass the commands in [[scripting#using_scripting|using command-line]]. 
 + 
 +===== .NET (C# or VB.NET) ===== 
 +See [[guide_dotnet|*]]. 
 + 
 +===== [[external]] External script interpreters (PowerShell, PHP, Perl, etc) =====
If you are familiar with other scripting languages, you can use those. 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 [[http://www.php.net/downloads.php|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.+All versions of Windows since Windows XP have PowerShell available. With PowerShell, it is more convenient to use [[library_powershell|WinSCP .NET assembly]], than the simple scripting. There are lot of [[library_examples|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 [[https://www.php.net/downloads.php|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.
-Now you can execute PHP script ''example.php'':+Having the two binaries you can execute PHP script ''example.php'':
<code php> <code php>
<? <?
Line 118: Line 135:
===== Further reading ===== ===== Further reading =====
-  * [[reporting|Troubleshooting]];+  * [[troubleshooting|Troubleshooting]]; 
 +  * [[library|WinSCP .NET assembly]];
  * [[scripting|Scripting]] documentation;   * [[scripting|Scripting]] documentation;
  * Guide to [[guide_automation|automation]];   * Guide to [[guide_automation|automation]];
-  * [[faq#scripting_automation|FAQ about scripting]];+  * [[faq#scripting|FAQ about scripting]];
  * Example [[scripts|scripts]].   * Example [[scripts|scripts]].

Last modified: by martin