AutoIt
AutoIt
Directory Existence Check
See more FTP Examples
How to test if a directory exists on an FTP server.A good way to check to see if a directory already exists is to try to "cd" to that remote directory by calling ChangeRemoteDir. If it succeeds, then the directory exists. If not, then it does not exist. An alternative method is to set the ListPattern = "*" and then iterate over the files/directories, looking for the directory.
Chilkat AutoIt Downloads
Local $bSuccess = False
; This example requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
$oFtp = ObjCreate("Chilkat.Ftp2")
$oFtp.Hostname = "ftp.example.com"
$oFtp.Username = "login"
$oFtp.Password = "password"
; Connect and login to the FTP server.
$bSuccess = $oFtp.Connect()
If ($bSuccess <> True) Then
ConsoleWrite($oFtp.LastErrorText & @CRLF)
Exit
EndIf
; Does the "temp" directory exist?
Local $bDirExists
$bDirExists = $oFtp.ChangeRemoteDir("/temp")
If ($bDirExists = True) Then
ConsoleWrite("Yes, the temp directory exists." & @CRLF)
; Yes, it exists. Restore the current remote dir:
$bSuccess = $oFtp.ChangeRemoteDir("..")
If ($bSuccess <> True) Then
ConsoleWrite($oFtp.LastErrorText & @CRLF)
Exit
EndIf
EndIf
; Alternatively, you may set the ListPattern = "*" and
; look for the directory:
$oFtp.ListPattern = "*"
Local $i
Local $iN
$iN = $oFtp.GetDirCount()
If ($iN < 0) Then
; Failed to get directory listing based on ListPattern
ConsoleWrite($oFtp.LastErrorText & @CRLF)
Exit
EndIf
If ($iN > 0) Then
For $i = 0 To $iN - 1
Local $bIsDir
$bIsDir = $oFtp.GetIsDirectory($i)
If ($bIsDir = True) Then
Local $sFname
$sFname = $oFtp.GetFilename($i)
If ($sFname = "temp") Then
ConsoleWrite("Found temp directory!" & @CRLF)
$bSuccess = $oFtp.Disconnect()
Exit
EndIf
EndIf
Next
EndIf
$bSuccess = $oFtp.Disconnect()