DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoSftp
Integer iPort
String sPassword
String sHandle
Variant vDirListing
Handle hoDirListing
Integer n
Variant vFileObj
Handle hoFileObj
Integer i
String sTemp1
String sTemp2
Boolean bTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatSFtp)) To hoSftp
If (Not(IsComObjectCreated(hoSftp))) Begin
Send CreateComObject of hoSftp
End
// Connect, authenticate, and initialize the SFTP subsystem.
Move 22 To iPort
Get ComConnect Of hoSftp "sftp.example.com" iPort To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Move "mySshPassword" To sPassword
Get ComAuthenticatePw Of hoSftp "mySshLogin" sPassword To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComInitializeSftp Of hoSftp To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Open the remote directory to be listed. OpenDir returns a handle string.
Get ComOpenDir Of hoSftp "subdir" To sHandle
Get ComLastMethodSuccess Of hoSftp To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Read all directory entries into an SFtpDir object.
Get Create (RefClass(cComChilkatSFtpDir)) To hoDirListing
If (Not(IsComObjectCreated(hoDirListing))) Begin
Send CreateComObject of hoDirListing
End
Get pvComObject of hoDirListing to vDirListing
Get ComReadDirListing Of hoSftp sHandle vDirListing To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// The listing has been read, so the directory handle can be closed now.
Get ComCloseHandle Of hoSftp sHandle To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoSftp To sTemp1
Showln sTemp1
Procedure_Return
End
// Iterate the entries. Entries are in server order (Chilkat does not sort the listing).
Get ComNumFilesAndDirs Of hoDirListing To n
Showln "Directory contains " n " entries:"
Get Create (RefClass(cComChilkatSFtpFile)) To hoFileObj
If (Not(IsComObjectCreated(hoFileObj))) Begin
Send CreateComObject of hoFileObj
End
For i From 0 To (n - 1)
// FileAt fills the SFtpFile object with the entry at the given index.
Get pvComObject of hoFileObj to vFileObj
Get ComFileAt Of hoDirListing i vFileObj To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoDirListing To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComIsDirectory Of hoFileObj To bTemp1
If (bTemp1) Begin
Get ComFilename Of hoFileObj To sTemp1
Showln " [DIR] " sTemp1
End
Else Begin
// Size64 is a 64-bit size; LastModifiedTimeStr is the modification time as text.
Get ComFilename Of hoFileObj To sTemp1
Get ComLastModifiedTimeStr Of hoFileObj To sTemp2
Showln " " sTemp1 " (" ERROR - invalid ATG type in VAC: int64 " bytes, modified " sTemp2 ")"
End
Loop
Send ComDisconnect To hoSftp
End_Procedure