Sample code for 30+ languages & platforms
Visual Basic 6.0

Download Text from an SFTP Server (StringBuilder)

See more SFTP Examples

Demonstrates the Chilkat SFtp.DownloadSb method, which downloads a remote text file, decodes it with the given charset, and appends the text to a StringBuilder. The arguments are the remote file path, the charset, and the StringBuilder.

Note: This example uses relative local paths, which are resolved against the application's current working directory. Absolute local paths may also be used. Supply the paths appropriate to your own environment.

Background: When the remote file is text and you want its content as characters rather than raw bytes, this decodes it in one step so you can parse or display it directly. The charset must match how the file is actually encoded, or the text will be garbled. Like DownloadBd it appends, so clear the StringBuilder first if you want only this file's text.

Chilkat Visual Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
success = 0

'  Demonstrates the SFtp.DownloadSb method, which downloads a remote text file, decodes it, and
'  appends the text to a StringBuilder.  The 1st argument is the remote file path, the 2nd is the
'  charset, and the 3rd is the StringBuilder.

Dim sftp As New ChilkatSFtp

'  Connect, authenticate, and initialize the SFTP subsystem.
Dim port As Long
port = 22
success = sftp.Connect("sftp.example.com",port)
If (success = 0) Then
    Debug.Print sftp.LastErrorText
    Exit Sub
End If

'  Normally you would not hard-code the password in source.  You should instead obtain it
'  from an interactive prompt, environment variable, or a secrets vault.
Dim password As String
password = "mySshPassword"

success = sftp.AuthenticatePw("mySshLogin",password)
If (success = 0) Then
    Debug.Print sftp.LastErrorText
    Exit Sub
End If

success = sftp.InitializeSftp()
If (success = 0) Then
    Debug.Print sftp.LastErrorText
    Exit Sub
End If

'  Download the remote text file, decoding it as UTF-8.  Existing text in the StringBuilder is
'  preserved, so clear it first if you want replacement rather than append behavior.
Dim sb As New ChilkatStringBuilder
success = sftp.DownloadSb("subdir/inventory.csv","utf-8",sb)
If (success = 0) Then
    Debug.Print sftp.LastErrorText
    Exit Sub
End If

Debug.Print "Characters downloaded: " & sb.Length
Dim fileText As String
fileText = sb.GetAsString()
Debug.Print fileText

sftp.Disconnect