Sample code for 30+ languages & platforms
SQL Server

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 SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  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.

    DECLARE @sftp int
    EXEC @hr = sp_OACreate 'Chilkat.SFtp', @sftp OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Connect, authenticate, and initialize the SFTP subsystem.
    DECLARE @port int
    SELECT @port = 22
    EXEC sp_OAMethod @sftp, 'Connect', @success OUT, 'sftp.example.com', @port
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    --  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.
    DECLARE @password nvarchar(4000)
    SELECT @password = 'mySshPassword'

    EXEC sp_OAMethod @sftp, 'AuthenticatePw', @success OUT, 'mySshLogin', @password
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    EXEC sp_OAMethod @sftp, 'InitializeSftp', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    --  When followLinks is 1, a symbolic link is followed and the target's type is returned.
    DECLARE @followLinks int
    SELECT @followLinks = 1
    DECLARE @result int
    EXEC sp_OAMethod @sftp, 'FileExists', @result OUT, 'subdir/report.pdf', @followLinks

    --  Interpret the return value.
    IF @result = -1
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    IF @result = 0
      BEGIN

        PRINT 'The path does not exist.'
      END

    IF @result = 1
      BEGIN

        PRINT 'A regular file exists.'
      END

    IF @result = 2
      BEGIN

        PRINT 'A directory exists.'
      END

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

    EXEC sp_OAMethod @sftp, 'Disconnect', NULL

    EXEC @hr = sp_OADestroy @sftp


END
GO