Differences
This shows you the differences between the selected revisions of the page.
2012-03-21 | 2012-03-21 | ||
guaranteed call to session.dispose from vbscript (martin) | try catch finally in jscript (martin) | ||
Line 167: | Line 167: | ||
<script language="JScript"> | <script language="JScript"> | ||
- | // Setup session options | + | try |
- | var sessionOptions = WScript.CreateObject("WinSCP.SessionOptions"); | + | { |
- | sessionOptions.Protocol = Protocol_Sftp; | + | ····// Setup session options |
- | sessionOptions.HostName = "example.com"; | + | ····var sessionOptions = WScript.CreateObject("WinSCP.SessionOptions"); |
- | sessionOptions.UserName = "user"; | + | ····sessionOptions.Protocol = Protocol_Sftp; |
- | sessionOptions.Password = "mypassword"; | + | ····sessionOptions.HostName = "example.com"; |
- | sessionOptions.SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"; | + | ····sessionOptions.UserName = "user"; |
+ | ····sessionOptions.Password = "mypassword"; | ||
+ | ····sessionOptions.SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"; | ||
- | var session = WScript.CreateObject("WinSCP.Session"); | + | ····var session = WScript.CreateObject("WinSCP.Session"); |
- | // Connect | + | try |
- | session.Open(sessionOptions); | + | { |
+ | ········// Connect | ||
+ | ········session.Open(sessionOptions); | ||
- | // Upload files | + | ········// Upload files |
- | var transferOptions = WScript.CreateObject("WinSCP.TransferOptions"); | + | ········var transferOptions = WScript.CreateObject("WinSCP.TransferOptions"); |
- | 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 |
- | transferResult.Check(); | + | ········transferResult.Check(); |
- | + | ········ | |
- | // Print results | + | ········// Print results |
- | for (var enumerator = new Enumerator(transferResult.Transfers); !enumerator.atEnd(); enumerator.moveNext()) | + | ········for (var enumerator = new Enumerator(transferResult.Transfers); !enumerator.atEnd(); enumerator.moveNext()) |
+ | { | ||
+ | WScript.Echo("Upload of " + enumerator.item().FileName + " succeeded"); | ||
+ | } | ||
+ | } | ||
+ | finally | ||
+ | { | ||
+ | // Disconnect, clean up | ||
+ | session.Dispose(); | ||
+ | } | ||
+ | } | ||
+ | catch (e) | ||
{ | { | ||
- | WScript.Echo("Upload of " + enumerator.item().FileName + " succeeded"); | + | WScript.Echo("Error: " + e.message); |
+ | ···WScript.Quit(1); | ||
} | } | ||
- | |||
- | // Disconnect, clean up | ||
- | session.Dispose(); | ||
</script> | </script> |