Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Keywords: Winsock WinInet SFTP FTP SSH WinSCP COM OLE dotNet .NET Assembly WinSCP.SessionOptions WinSCP.Session Protocol SshHostKeyFingerprint
Using dotNet with WinSCP: http://winscp.net/eng/docs/guide_dotnet
I put together a WinBatch dotNet code sample that uses WinSCP to handle SFTP.
REQUIRES WinBatch 2013A or newer Before starting you should: - Download and install both WinSCP and its .NET assembly/COM library (http://winscp.net/eng/download.php) By default WinScp installs to C:\Program Files (x86)\WinSCP you will need to copy the WinSCp.dll to this directory - Know how to connect to your FTP/SFTP account;Here is the code. You will need to modify for your environment...
;*************************************************************************** ;** SFTP using WinSCP dotNet ;** ;** Purpose: SFTP using WinSCP dotNet ;** Inputs: ;** Outputs: ;** Reference: ;** REQUIRES WinBatch 2013A or newer ;** http://winscp.net/eng/docs/guide_dotnet ;** http://winscp.net/eng/docs/library (WinSCP .NET Assembly and COM Library) ;** Before starting you should: ;** - Download and install both WinSCP and its .NET assembly/COM library (http://winscp.net/eng/download.php) ;** By default WinScp installs to C:\Program Files (x86)\WinSCP you will need to copy the WinSCp.dll to this directory ;** - Know how to connect to your FTP/SFTP account; ;** - Know what WinSCP scripting commands to use for your task (e.g. file transfer). ;** ;** Developer: Deana Falk 2013.10.31 ;*************************************************************************** If Version( )< '2013A' Pause( 'Notice', 'Need 2013A or Newer Version of WinBatch' ) Exit EndIf assemblydir = 'C:\Program Files (x86)\WinSCP' ObjectClrOption( 'appbase', assemblydir ) ObjectClrOption ( 'use', 'WinSCP, Version=1.0.7.3446, Culture=neutral' ) ; Confirm version matches the dll ;ObjectClrOption('use','WinSCPnet, Version=1.1.6.4433, Culture=neutral') ; Confirm version matches the dll ;Create an instance of the WinSCP.SessionOptions class and fill in all necessary information to allow an automatic connection and authentication of your session. objSessionOptions = ObjectClrNew( 'WinSCP.SessionOptions' ) ; Define Protocol ; Possible values are Protocol.Sftp (default), Protocol.Scp and Protocol.Ftp. objProtocol = ObjectClrNew( 'WinSCP.Protocol' ) Sftp = ObjectClrType( 'WinSCP.Protocol', objProtocol.Sftp ) ; Sftp ;Ftp = ObjectClrType( 'WinSCP.Protocol', objProtocol.Ftp ) ; Ftp ; MODIFY TO FIT YOUR NEEDS ;Scp = ObjectClrType( 'WinSCP.Protocol', objProtocol.Scp ) ; Scp objSessionOptions.Protocol = Sftp ; Sftp, Ftp or Scp ; MODIFY TO FIT YOUR NEEDS objSessionOptions.HostName = 'ftp.example.com' ; MODIFY TO FIT YOUR NEEDS objSessionOptions.UserName = 'user' ; MODIFY TO FIT YOUR NEEDS objSessionOptions.Password = 'password' ; MODIFY TO FIT YOUR NEEDS If IsDefined(Sftp) objSessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" ;REQUIRED by SFTP ; MODIFY TO FIT YOUR NEEDS EndIf ;Create an instance of the WinSCP.Session class. Optionally you can hook handlers of some events of the class. objSession = ObjectClrNew("WinSCP.Session" ) ;.Open the session using Session.Open method, passing instance of your WinSCP.SessionOptions. objSession.Open(objSessionOptions) ;Once the session is opened, you can use any of the WinSCP.Session methods to manipulate remote files, e.g., ;objSession.GetFiles to download files, ;objSession.PutFiles to upload files or ;objSession.SynchronizeDirectories to synchronize directories. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; GetFiles sample ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ; Force binary mode transfer objTransferOptions = ObjectClrNew( 'WinSCP.TransferOptions' ) objTransferMode = ObjectClrNew( 'WinSCP.TransferMode' ) objTransferOptions.TransferMode = ObjectClrType( 'WinSCP.TransferMode', objTransferMode.Binary ) ; Download file to the local directory - Note use of absolute path bFalse = ObjectType( 'BOOL', 0 ) transferResult = objSession.GetFiles( '/wwwftp/2013/Readme.txt', 'c:\temp\', bFalse, objTransferOptions) ; MODIFY TO FIT YOUR NEEDS ; Disconnect, clean up objSession.Dispose() objTransferOptions = 0 objSession = 0 objProtocol = 0 objSessionOptions = 0 Exit
Article ID: W17858
Filename: SFTP using WinSCP dotNet .txt
File Created: 2016:04:07:13:49:20
Last Updated: 2016:04:07:13:49:20