Sample code for 30+ languages & platforms
AutoIt

SFTP Read Directory Listing

See more SFTP Examples

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

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; 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.

$oSftp = ObjCreate("Chilkat.SFtp")

; Set some timeouts, in milliseconds:
$oSftp.ConnectTimeoutMs = 5000
$oSftp.IdleTimeoutMs = 10000

; Connect to the SSH server.  
; The standard SSH port = 22
; The hostname may be a hostname or IP address.
Local $sHostname = "www.my-sftp-server.com"
Local $iPort = 22
$bSuccess = $oSftp.Connect($sHostname,$iPort)
If ($bSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

; Authenticate with the SSH server.  Chilkat SFTP supports
; both password-based authenication as well as public-key
; authentication.  This example uses password authenication.
$bSuccess = $oSftp.AuthenticatePw("myLogin","myPassword")
If ($bSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

; After authenticating, the SFTP subsystem must be initialized:
$bSuccess = $oSftp.InitializeSftp()
If ($bSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

; 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. 
Local $sHandle = $oSftp.OpenDir(".")
If ($oSftp.LastMethodSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

; Download the directory listing:

$oDirListing = ObjCreate("Chilkat.SFtpDir")
$bSuccess = $oSftp.ReadDirListing($sHandle,$oDirListing)
If ($bSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

; Close the handle for the directory listing.
$bSuccess = $oSftp.CloseHandle($sHandle)
If ($bSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

; Iterate over the files.
$oFileObj = ObjCreate("Chilkat.SFtpFile")
Local $i = 0
Local $iN = $oDirListing.NumFilesAndDirs
While $i < $iN

    $bSuccess = $oDirListing.FileAt($i,$oFileObj)
    If ($bSuccess = False) Then
        ConsoleWrite($oDirListing.LastErrorText & @CRLF)
        Exit
    EndIf

    ConsoleWrite($oFileObj.Filename & @CRLF)
    ConsoleWrite($oFileObj.FileType & @CRLF)
    ConsoleWrite("Size in bytes: " & $oFileObj.Size32 & @CRLF)
    ConsoleWrite("----" & @CRLF)

    $i = $i + 1
Wend

ConsoleWrite("Success." & @CRLF)