Differences
This shows you the differences between the selected revisions of the page.
2012-03-21 | 2012-03-21 | ||
calling dispose (martin) | guaranteed call to session.dispose from vbscript (martin) | ||
Line 105: | Line 105: | ||
</code> | </code> | ||
- | If you do not want to test for errors after every statement, you need to group the staments you want to guard into a subprocedure and enable custom error handling before calling/entering the subprocedure: | + | If you do not want to test for errors after every statement, you need to group the staments you want to guard into a subprocedure and enable custom error handling before calling/entering the subprocedure. |
+ | |||
+ | This approach is also recommended to ensure that ''[[library_session_dispose|Session.Dispose]]'' is called even in case of error. | ||
<code vb> | <code vb> | ||
- | Sub ListDirectory | + | Sub ListDirectory(ByRef session) |
+ | ' Setup session options | ||
... | ... | ||
+ | |||
+ | ' Connect | ||
session.Open sessionOptions | session.Open sessionOptions | ||
Dim directoryInfo | Dim directoryInfo | ||
Set directoryInfo = session.ListDirectory("/home/user/") | Set directoryInfo = session.ListDirectory("/home/user/") | ||
+ | |||
+ | ' Do some stuff with directory listing | ||
... | ... | ||
End Sub | End Sub | ||
+ | |||
+ | Dim session | ||
+ | Set session = WScript.CreateObject("WinSCP.Session") | ||
' Enable custom error handling | ' Enable custom error handling | ||
Line 122: | Line 132: | ||
' Now, with custom error handling enabled before calling the ListDirectory subprocedure, | ' Now, with custom error handling enabled before calling the ListDirectory subprocedure, | ||
' any statement in the subprocedure terminates the subprocedure (but not the whole script) | ' any statement in the subprocedure terminates the subprocedure (but not the whole script) | ||
- | ListDirectory | + | ListDirectory session |
' Query for errors | ' Query for errors | ||
If Err.Number <> 0 Then | If Err.Number <> 0 Then | ||
WScript.Echo "Listing directory failed: " & Err.Description | WScript.Echo "Listing directory failed: " & Err.Description | ||
+ | |||
+ | ' Disconnect, clean up | ||
+ | session.Dispose | ||
+ | |||
WScript.Quit 1 | WScript.Quit 1 | ||
End If | End If | ||
Line 132: | Line 146: | ||
' Clear the error | ' Clear the error | ||
Err.Clear | Err.Clear | ||
+ | |||
+ | ' Disconnect, clean up | ||
+ | session.Dispose | ||
' Restore default error handling | ' Restore default error handling | ||
On Error GoTo 0 | On Error GoTo 0 | ||
+ | |||
+ | ' Now with session cleanly closed, safely do anything unrelated to WinSCP session | ||
+ | ... | ||
</code> | </code> | ||