Sample code for 30+ languages & platforms
C

Check if a Remote File or Directory Exists

See more SFTP Examples

Demonstrates the Chilkat SFtp.FileExists method, which checks a remote path and returns an integer describing what is there. The first argument is the remote path and the second (followLinks) selects whether a symbolic link is followed to its target. The return value is -1 on error, 0 if nothing exists, 1 for a file, 2 for a directory, 3 for a symbolic link, and higher values for special entry types.

Background: This returns more than a yes/no because "does it exist" usually comes with "and is it the kind of thing I expect" — a job that means to write a file should notice if a directory already occupies the path. The followLinks flag decides whether a symbolic link is reported as a link (3, only when not following) or transparently resolved to its target's type. Always handle the -1 case: it means the check itself failed, which is different from the path not existing.

Chilkat C Downloads

C
#include <C_CkSFtp.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSFtp sftp;
    int port;
    const char *password;
    BOOL followLinks;
    int result;

    success = FALSE;

    //  Demonstrates the SFtp.FileExists method, which checks a remote path and returns an integer
    //  describing what is there.  The 1st argument is the remote path and the 2nd (followLinks)
    //  selects whether a symbolic link is followed to its target.

    sftp = CkSFtp_Create();

    //  Connect, authenticate, and initialize the SFTP subsystem.
    port = 22;
    success = CkSFtp_Connect(sftp,"sftp.example.com",port);
    if (success == FALSE) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        return;
    }

    //  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.
    password = "mySshPassword";

    success = CkSFtp_AuthenticatePw(sftp,"mySshLogin",password);
    if (success == FALSE) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        return;
    }

    success = CkSFtp_InitializeSftp(sftp);
    if (success == FALSE) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        return;
    }

    //  When followLinks is TRUE, a symbolic link is followed and the target's type is returned.
    followLinks = TRUE;
    result = CkSFtp_FileExists(sftp,"subdir/report.pdf",followLinks);

    //  Interpret the return value.
    if (result == -1) {
        printf("%s\n",CkSFtp_lastErrorText(sftp));
        CkSFtp_Dispose(sftp);
        return;
    }

    if (result == 0) {
        printf("The path does not exist.\n");
    }

    if (result == 1) {
        printf("A regular file exists.\n");
    }

    if (result == 2) {
        printf("A directory exists.\n");
    }

    //  Other possible values: 3 = symbolic link (only when followLinks is FALSE), 4 = special
    //  entry, 5 = unknown, 6 = socket, 7 = character device, 8 = block device, 9 = FIFO.

    CkSFtp_Disconnect(sftp);


    CkSFtp_Dispose(sftp);

    }