Sample code for 30+ languages & platforms
Unicode C

Delete Remote FTP Files Missing Locally (Sync)

See more FTP Examples

Demonstrates the Chilkat Ftp2.SyncDeleteRemote method, which recursively compares the remote tree with a local tree and deletes remote files that have no corresponding local file. The only argument is the local root.

Note: The local paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: This provides the "delete extras" half of a true mirror: SyncRemoteTree adds and updates, and SyncDeleteRemote removes remote files that no longer exist in the source, so together they make the server match the local directory exactly. Because it deletes, run it deliberately — a preview upload with SyncRemoteTree2 first, and confidence that the local tree is the intended source of truth, guard against removing files you meant to keep.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkFtp2W.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkFtp2W ftp;

    success = FALSE;

    //  Demonstrates the Ftp2.SyncDeleteRemote method, which recursively compares the remote tree with a
    //  local tree and deletes remote files that have no corresponding local file.  The only argument
    //  is the local root.

    ftp = CkFtp2W_Create();

    CkFtp2W_putHostname(ftp,L"ftp.example.com");
    CkFtp2W_putUsername(ftp,L"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.
    CkFtp2W_putPassword(ftp,L"myPassword");

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

    success = CkFtp2W_ChangeRemoteDir(ftp,L"public_html");
    if (success == FALSE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        return;
    }

    //  This makes the remote tree match the local tree by removing remote-only files.  Combine it
    //  with SyncRemoteTree (upload) to make the remote an exact mirror of the local directory.
    success = CkFtp2W_SyncDeleteRemote(ftp,L"qa_data/site");
    if (success == FALSE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        return;
    }

    wprintf(L"Removed remote files not present locally.\n");

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



    CkFtp2W_Dispose(ftp);

    }