Differences

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

guide_automation_advanced 2012-03-24 guide_automation_advanced 2022-10-21 (current)
Line 1: Line 1:
====== Advanced FTP/SFTP scripting ====== ====== Advanced FTP/SFTP scripting ======
-**//[[library|WinSCP .NET assembly]] mostly deprecates techniques demostrated in this guide. Using the assembly is now preferred approach for advanced automation tasks with WinSCP.//**+&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 (''[[http://technet.microsoft.com/en-us/library/bb490887.aspx|cscript.exe]]'' or ''[[http://technet.microsoft.com/en-us/library/bb490816.aspx|wscript.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.
===== [[wsh]] Windows script host (JScript 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 greater 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 ''[[http://technet.microsoft.com/en-us/library/bb490887.aspx|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>
==== [[inout]] Access to Input/Output streams ==== ==== [[inout]] Access to Input/Output streams ====
-You can use ''[[msdn&gt;aew9yb99|WScript.Shell]]'' to execute WinSCP [[executables|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(
Line 47: Line 49:
</code> </code>
-==== XML parsing ==== +==== [[wsh_xml_parsing]] XML parsing ==== 
-You can use ''[[msdn&gt;ms756987|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 90: Line 92:
</code> </code>
-//See also guide to [[guide_interpreting_xml_log|interpreting XML log for advanced scripting]] (uses C# language, though could be easily rewritten to JScript or VB script).//+//See also the guide [[guide_interpreting_xml_log|*]] (uses C# language, though could be easily rewritten to JScript or VB script).//
==== Hidden execution ==== ==== Hidden execution ====
-When executing JavaScript code with ''[[http://technet.microsoft.com/en-us/library/bb490887.aspx|cscript.exe]]'', console window is shown. If you want to execute the JavaScript without showing console window, use ''[[http://technet.microsoft.com/en-us/library/bb490816.aspx|wscript.exe]]'' instead.+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'': 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).   * 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 [[scripting#using_scripting|using command-line]].+  * 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) ===== ===== .NET (C# or VB.NET) =====
-See [[guide_dotnet|guide to using WinSCP from .NET]].+See [[guide_dotnet|*]].
-===== External script interpreters (PHP, Perl, etc) =====+===== [[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. No installing or registration is required.+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.
Having the two binaries you can execute PHP script ''example.php'': Having the two binaries you can execute PHP script ''example.php'':
Line 135: Line 139:
  * [[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