Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoFtp
    Boolean iDirExists
    Integer i
    Integer n
    Boolean iIsDir
    String sFname
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatFtp2)) To hoFtp
    If (Not(IsComObjectCreated(hoFtp))) Begin
        Send CreateComObject of hoFtp
    End

    Set ComHostname Of hoFtp To "ftp.example.com"
    Set ComUsername Of hoFtp To "login"
    Set ComPassword Of hoFtp To "password"

    // Connect and login to the FTP server.
    Get ComConnect Of hoFtp To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Does the "temp" directory exist?

    Get ComChangeRemoteDir Of hoFtp "/temp" To iDirExists
    If (iDirExists = True) Begin
        Showln "Yes, the temp directory exists."
        //  Yes, it exists. Restore the current remote dir:
        Get ComChangeRemoteDir Of hoFtp ".." To iSuccess
        If (iSuccess <> True) Begin
            Get ComLastErrorText Of hoFtp To sTemp1
            Showln sTemp1
            Procedure_Return
        End

    End

    // Alternatively, you may set the ListPattern = "*" and 
    //  look for the directory:
    Set ComListPattern Of hoFtp To "*"

    Get ComGetDirCount Of hoFtp To n
    If (n < 0) Begin
        // Failed to get directory listing based on ListPattern
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    If (n > 0) Begin
        For i From 0 To (n - 1)

            Get ComGetIsDirectory Of hoFtp i To iIsDir
            If (iIsDir = True) Begin

                Get ComGetFilename Of hoFtp i To sFname
                If (sFname = "temp") Begin
                    Showln "Found temp directory!"
                    Get ComDisconnect Of hoFtp To iSuccess
                    Procedure_Return
                End

            End

        Loop

    End

    Get ComDisconnect Of hoFtp To iSuccess


End_Procedure