PowerBuilder
PowerBuilder
List a Remote Directory on an SFTP Server
See more SFTP Examples
Demonstrates the Chilkat SFtp.ReadDirListing method, which reads all remaining entries from an open directory handle into an SFtpDir object. The first argument is the handle returned by OpenDir and the second is the SFtpDir that receives the listing. The example iterates the entries with NumFilesAndDirs and FileAt, reading each SFtpFile's Filename, IsDirectory, Size64, and LastModifiedTimeStr.
Background: Listing a directory is a three-part operation:
OpenDir gets a handle, ReadDirListing pulls every entry into an SFtpDir in one call, and the handle is then closed. Each entry is an SFtpFile carrying that item's name, type, size, timestamps, owner, and permission flags — a single stat covering what several separate GetFile* calls would return. The entries come back in the server's own order rather than sorted; use SFtpDir.Sort if you need a particular ordering. Note that a listing normally includes the . and .. entries, which most code skips.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Sftp
integer li_Port
string ls_Password
string ls_Handle
oleobject loo_DirListing
integer n
oleobject loo_FileObj
integer i
li_Success = 0
// Demonstrates the SFtp.ReadDirListing method, which reads all remaining entries from an open
// directory handle into an SFtpDir object. The 1st argument is the handle (from OpenDir) and
// the 2nd is the SFtpDir that receives the listing.
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
// Connect, authenticate, and initialize the SFTP subsystem.
li_Port = 22
li_Success = loo_Sftp.Connect("sftp.example.com",li_Port)
if li_Success = 0 then
Write-Debug loo_Sftp.LastErrorText
destroy loo_Sftp
return
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.
ls_Password = "mySshPassword"
li_Success = loo_Sftp.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
Write-Debug loo_Sftp.LastErrorText
destroy loo_Sftp
return
end if
li_Success = loo_Sftp.InitializeSftp()
if li_Success = 0 then
Write-Debug loo_Sftp.LastErrorText
destroy loo_Sftp
return
end if
// Open the remote directory to be listed. OpenDir returns a handle string.
ls_Handle = loo_Sftp.OpenDir("subdir")
if loo_Sftp.LastMethodSuccess = 0 then
Write-Debug loo_Sftp.LastErrorText
destroy loo_Sftp
return
end if
// Read all directory entries into an SFtpDir object.
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
// The listing has been read, so the directory handle can be closed now.
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 the entries. Entries are in server order (Chilkat does not sort the listing).
n = loo_DirListing.NumFilesAndDirs
Write-Debug "Directory contains " + string(n) + " entries:"
loo_FileObj = create oleobject
li_rc = loo_FileObj.ConnectToNewObject("Chilkat.SFtpFile")
for i = 0 to n - 1
// FileAt fills the SFtpFile object with the entry at the given index.
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
if loo_FileObj.IsDirectory then
Write-Debug " [DIR] " + loo_FileObj.Filename
else
// Size64 is a 64-bit size; LastModifiedTimeStr is the modification time as text.
Write-Debug " " + loo_FileObj.Filename + " (" + string(loo_FileObj.Size64) + " bytes, modified " + loo_FileObj.LastModifiedTimeStr + ")"
end if
next
loo_Sftp.Disconnect()
destroy loo_Sftp
destroy loo_DirListing
destroy loo_FileObj