Sample code for 30+ languages & platforms
DataFlex

Count Files in a Remote FTP Directory

See more FTP Examples

Demonstrates the Chilkat Ftp2.GetDirCount method, which returns the number of files and directories in the current remote directory. It takes no arguments; the first call retrieves the listing, and the count reflects the case-insensitive ListPattern filter. A negative return indicates failure.

Background: GetDirCount is the entry point to Chilkat's index-based directory API: its first call fetches the listing for the current directory into a cache, and the returned count establishes the valid index range (0 through count - 1) for the accessor methods that read names, sizes, and attributes. The ListPattern property narrows the listing to matching names before counting, which is how you list, say, only *.html without filtering in your own code.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoFtp
    Integer n
    Integer i
    String sName
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the Ftp2.GetDirCount method, which returns the number of files and directories in
    //  the current remote directory's listing.  It takes no arguments.  The first call retrieves the
    //  listing; the count reflects the case-insensitive ListPattern filter.

    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 "myFtpLogin"

    //  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.
    Set ComPassword Of hoFtp To "myPassword"

    Get ComConnect Of hoFtp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComChangeRemoteDir Of hoFtp "public_html" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Optionally filter the listing with a wildcard pattern.  "*" (the default) matches everything.
    Set ComListPattern Of hoFtp To "*.html"

    Get ComGetDirCount Of hoFtp To n
    If (n < 0) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Matching entries: " n

    //  The count defines the valid index range 0 through n-1 for the indexed accessor methods.

    For i From 0 To (n - 1)
        Get ComGetFilename Of hoFtp i To sName
        Showln sName
    Loop

    Get ComDisconnect Of hoFtp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure