Sample code for 30+ languages & platforms
Unicode C

FTP Iterate over Files in Directory Matching ListPattern

See more RSA Examples

Uses the ListPattern property to iterate over the files in a directory matching the pattern.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkFtp2W.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkFtp2W ftp;
    int n;
    int i;
    const wchar_t *filename;
    HCkStringBuilderW sbLocalPath;

    success = FALSE;

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

    ftp = CkFtp2W_Create();

    CkFtp2W_putHostname(ftp,L"ftp.example.com");
    CkFtp2W_putUsername(ftp,L"my_login");
    CkFtp2W_putPassword(ftp,L"my_password");
    CkFtp2W_putPort(ftp,21);
    CkFtp2W_putAuthTls(ftp,TRUE);

    success = CkFtp2W_Connect(ftp);
    if (success != TRUE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        return;
    }

    // Change to the "images" sub-directory located under our FTP account's home directory.
    success = CkFtp2W_ChangeRemoteDir(ftp,L"images");
    if (success != TRUE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        return;
    }

    CkFtp2W_putListPattern(ftp,L"*.png");

    // Fetch the current remote directory contents by calling GetDirCount
    n = CkFtp2W_GetDirCount(ftp);
    if (n < 0) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        return;
    }

    i = 0;

    sbLocalPath = CkStringBuilderW_Create();

    while (i < n) {
        filename = CkFtp2W_getFilename(ftp,i);
        wprintf(L"%s\n",filename);

        // Download this file.
        CkStringBuilderW_SetString(sbLocalPath,L"qa_output/");
        CkStringBuilderW_Append(sbLocalPath,filename);
        success = CkFtp2W_GetFile(ftp,filename,CkStringBuilderW_getAsString(sbLocalPath));
        if (success != TRUE) {
            wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
            CkFtp2W_Dispose(ftp);
            CkStringBuilderW_Dispose(sbLocalPath);
            return;
        }

        i = i + 1;
    }



    CkFtp2W_Dispose(ftp);
    CkStringBuilderW_Dispose(sbLocalPath);

    }