Differences

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

library_com_wsh 2012-03-21 library_com_wsh 2022-10-21 (current)
Line 1: Line 1:
====== Using WinSCP .NET Assembly from WSH-hosted Active Scripting Languages ====== ====== Using WinSCP .NET Assembly from WSH-hosted Active Scripting Languages ======
-&beta_feature 
===== Installing and Registering for COM ===== ===== Installing and Registering for COM =====
Line 11: Line 10:
==== [[enums]] Accessing Enumeration Values ==== ==== [[enums]] Accessing Enumeration Values ====
-As JScript and VBScript access COM classes via ''IDispatch'', they do not make use of type library, hence they do not have direct access to constants defined there, like ''[[library_sessionoptions#properties|Protocol.Sftp]]'' for instance. To use these, you have to instruct WSH to import the type library into the script namespace.+As JScript and VBScript access COM classes via ''IDispatch'', they do not make use of type library, hence they do not have direct access to constants defined there, like ''[[library_sessionoptions#protocol|Protocol.Sftp]]'' for instance. To use these, you have to instruct WSH to import the type library into the script namespace.
You can use [[wp>Windows_Script_File|Windows Script File]] (WSF) and its ''<reference>'' tag for that. It makes WSH import all //enums// from the assembly type library into constants in script namespace with name like ''<type>_<member>'', e.g. ''Protocol.Sftp'' becomes ''Protocol_Sftp''. You can use [[wp>Windows_Script_File|Windows Script File]] (WSF) and its ''<reference>'' tag for that. It makes WSH import all //enums// from the assembly type library into constants in script namespace with name like ''<type>_<member>'', e.g. ''Protocol.Sftp'' becomes ''Protocol_Sftp''.
Line 27: Line 26:
sessionOptions.UserName = "user"; sessionOptions.UserName = "user";
sessionOptions.Password = "mypassword"; sessionOptions.Password = "mypassword";
-sessionOptions.SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx";+sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...";
var session = WScript.CreateObject("WinSCP.Session"); var session = WScript.CreateObject("WinSCP.Session");
Line 40: Line 39:
You run the ''.wsf'' file the same way as you run ''.js'' or ''.vbs'': You run the ''.wsf'' file the same way as you run ''.js'' or ''.vbs'':
-<code>+<code batch>
cscript upload.wsf cscript upload.wsf
</code> </code>
-==== Event Handlers ====+==== [[event_handlers]] Event Handlers ====
The ''[[library_session|Session]]'' class exposes several [[library_session#events|events]]. The ''[[library_session|Session]]'' class exposes several [[library_session#events|events]].
Line 61: Line 60:
var session = WScript.CreateObject("WinSCP.Session", "session_"); var session = WScript.CreateObject("WinSCP.Session", "session_");
</code> </code>
 +
 +For a full example, see an example for ''[[library_session_synchronizedirectories#jscript|Session.SynchronizeDirectories]]''.
And equivalent in VBScript: And equivalent in VBScript:
Line 71: Line 72:
Set session = WScript.CreateObject("WinSCP.Session", "session_") Set session = WScript.CreateObject("WinSCP.Session", "session_")
</code> </code>
 +
==== [[vberror]] Error Handling in VBScript ==== ==== [[vberror]] Error Handling in VBScript ====
VBScript does not support catching exceptions, what is a common way of handling errors in examples for most other languages. VBScript does not support catching exceptions, what is a common way of handling errors in examples for most other languages.
-In case you need to use custom error handling, instead of aborting the script on error (the default for WSH), use ''[[http://msdn.microsoft.com/en-us/library/53f3k80h(v=vs.85).aspx|On Error]]'' statement.+In case you need to use custom error handling, instead of aborting the script on error (the default for WSH), use ''[[https://learn.microsoft.com/en-us/previous-versions/53f3k80h(v=vs.85)|On Error]]'' statement.
-Use ''On Error Resume Next'' to disable default error handling. Then you need to query ''[[http://msdn.microsoft.com/en-us/library/sbf5ze0e(v=vs.85).aspx|Err.Number]]'' after every statement to test for errors. You can revert to default error handling (aborting the script) using ''On Error GoTo 0''.+Use ''On Error Resume Next'' to disable default error handling. Then you need to query ''[[https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/err-object|Err.Number]]'' after every statement to test for errors. You can revert to default error handling (aborting the script) using ''On Error GoTo 0''.
<code vb> <code vb>
Line 92: Line 94:
    WScript.Quit 1     WScript.Quit 1
End If End If
- 
-' Clear the error 
-Err.Clear 
' Restore default error handling ' Restore default error handling
Line 143: Line 142:
    WScript.Quit 1     WScript.Quit 1
End If End If
- 
-' Clear the error 
-Err.Clear 
' Disconnect, clean up ' Disconnect, clean up
Line 175: Line 171:
    sessionOptions.UserName = "user";     sessionOptions.UserName = "user";
    sessionOptions.Password = "mypassword";     sessionOptions.Password = "mypassword";
-    sessionOptions.SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx";+    sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...";
    var session = WScript.CreateObject("WinSCP.Session");     var session = WScript.CreateObject("WinSCP.Session");
Line 188: Line 184:
        transferOptions.TransferMode = TransferMode_Binary;         transferOptions.TransferMode = TransferMode_Binary;
-        var transferResult = session.PutFiles("d:\\toupload\\*", "/home/user/", false, transferOptions);+        var transferResult = 
 + ···········session.PutFiles("d:\\toupload\\*", "/home/user/", false, transferOptions);
               
        // Throw on any error         // Throw on any error
Line 194: Line 191:
               
        // Print results         // Print results
-        for (var enumerator = new Enumerator(transferResult.Transfers); !enumerator.atEnd(); enumerator.moveNext())+        var enumerator = new Enumerator(transferResult.Transfers)
 +        for (; !enumerator.atEnd(); enumerator.moveNext())
        {         {
            WScript.Echo("Upload of " + enumerator.item().FileName + " succeeded");             WScript.Echo("Upload of " + enumerator.item().FileName + " succeeded");
Line 216: Line 214:
===== [[vbscript]] VBScript Example ===== ===== [[vbscript]] VBScript Example =====
-This example is functionally equivalent to [[library#example|overall C# example for WinSCP .NET assembly]], except for [[library_com_wsh#vberror|error handling]].+This example is functionally equivalent to [[library#example|overall C# example for WinSCP .NET assembly]], except for [[#vberror|error handling]].
There are also [[library_examples|other VBScript examples]]. There are also [[library_examples|other VBScript examples]].
Line 235: Line 233:
    .UserName = "user"     .UserName = "user"
    .Password = "mypassword"     .Password = "mypassword"
-    .SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"+    .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
End With End With

Last modified: by martin