Sample code for 30+ languages & platforms
PowerBuilder

SFTP Read Directory Listing

See more SFTP Examples

Demonstrates how to download a directory listing and iterate over the files.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Sftp
string ls_Hostname
integer li_Port
string ls_Handle
oleobject loo_DirListing
oleobject loo_FileObj
integer i
integer n

li_Success = 0

// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// Important: It is helpful to send the contents of the
// sftp.LastErrorText property when requesting support.

loo_Sftp = create oleobject
li_rc = loo_Sftp.ConnectToNewObject("Chilkat.SFtp")
if li_rc < 0 then
    destroy loo_Sftp
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Set some timeouts, in milliseconds:
loo_Sftp.ConnectTimeoutMs = 5000
loo_Sftp.IdleTimeoutMs = 10000

// Connect to the SSH server.  
// The standard SSH port = 22
// The hostname may be a hostname or IP address.
ls_Hostname = "www.my-sftp-server.com"
li_Port = 22
li_Success = loo_Sftp.Connect(ls_Hostname,li_Port)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

// Authenticate with the SSH server.  Chilkat SFTP supports
// both password-based authenication as well as public-key
// authentication.  This example uses password authenication.
li_Success = loo_Sftp.AuthenticatePw("myLogin","myPassword")
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

// After authenticating, the SFTP subsystem must be initialized:
li_Success = loo_Sftp.InitializeSftp()
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

// Open a directory on the server...
// Paths starting with a slash are "absolute", and are relative 
// to the root of the file system. Names starting with any other 
// character are relative to the user's default directory (home directory).
// A path component of ".." refers to the parent directory, 
// and "." refers to the current directory. 
ls_Handle = loo_Sftp.OpenDir(".")
if loo_Sftp.LastMethodSuccess = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

// Download the directory listing:

loo_DirListing = create oleobject
li_rc = loo_DirListing.ConnectToNewObject("Chilkat.SFtpDir")

li_Success = loo_Sftp.ReadDirListing(ls_Handle,loo_DirListing)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    destroy loo_DirListing
    return
end if

// Close the handle for the directory listing.
li_Success = loo_Sftp.CloseHandle(ls_Handle)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    destroy loo_DirListing
    return
end if

// Iterate over the files.
loo_FileObj = create oleobject
li_rc = loo_FileObj.ConnectToNewObject("Chilkat.SFtpFile")

i = 0
n = loo_DirListing.NumFilesAndDirs
do while i < n

    li_Success = loo_DirListing.FileAt(i,loo_FileObj)
    if li_Success = 0 then
        Write-Debug loo_DirListing.LastErrorText
        destroy loo_Sftp
        destroy loo_DirListing
        destroy loo_FileObj
        return
    end if

    Write-Debug loo_FileObj.Filename
    Write-Debug loo_FileObj.FileType
    Write-Debug "Size in bytes: " + string(loo_FileObj.Size32)
    Write-Debug "----"

    i = i + 1
loop

Write-Debug "Success."


destroy loo_Sftp
destroy loo_DirListing
destroy loo_FileObj