Sample code for 30+ languages & platforms
SQL Server

SFTP Get File Date/Times in Different Formats

See more SFTP Examples

Demonstrates how to get remote file date/times in different formats.

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 @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

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

    -- Connect to the SSH server.  
    DECLARE @hostname nvarchar(4000)
    SELECT @hostname = 'my-sftp-server.com'
    DECLARE @port int
    SELECT @port = 22
    EXEC sp_OAMethod @sftp, 'Connect', @success OUT, @hostname, @port
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    -- Authenticate with the SSH server. 
    EXEC sp_OAMethod @sftp, 'AuthenticatePw', @success OUT, 'myLogin', 'myPassword'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    -- After authenticating, the SFTP subsystem must be initialized:
    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

    -- Open a directory on the server...
    -- This example opens the "junk" directory located under the HOME directory of the SSH user account.
    DECLARE @handle nvarchar(4000)
    EXEC sp_OAMethod @sftp, 'OpenDir', @handle OUT, 'junk'
    EXEC sp_OAGetProperty @sftp, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        RETURN
      END

    -- Download the directory listing:

    DECLARE @dirListing int
    EXEC @hr = sp_OACreate 'Chilkat.SFtpDir', @dirListing OUT

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

    -- Close the directory handle
    EXEC sp_OAMethod @sftp, 'CloseHandle', @success OUT, @handle
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @sftp
        EXEC @hr = sp_OADestroy @dirListing
        RETURN
      END

    -- Iterate over the files.
    -- Examine each filename and indicate those that match *FICHERO*.pdf
    -- (i.e. the filename contains the word "FICHERO" and ends in ".pdf")
    DECLARE @fileObj int
    EXEC @hr = sp_OACreate 'Chilkat.SFtpFile', @fileObj OUT

    DECLARE @sbFilename int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbFilename OUT

    DECLARE @caseSensitive int
    SELECT @caseSensitive = 0
    DECLARE @bLocalDateTime int
    SELECT @bLocalDateTime = 0

    DECLARE @dt int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @dt OUT

    DECLARE @i int
    SELECT @i = 0
    DECLARE @n int
    EXEC sp_OAGetProperty @dirListing, 'NumFilesAndDirs', @n OUT
    WHILE @i < @n
      BEGIN

        EXEC sp_OAMethod @dirListing, 'FileAt', @success OUT, @i, @fileObj
        IF @success = 0
          BEGIN
            EXEC sp_OAGetProperty @dirListing, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @sftp
            EXEC @hr = sp_OADestroy @dirListing
            EXEC @hr = sp_OADestroy @fileObj
            EXEC @hr = sp_OADestroy @sbFilename
            EXEC @hr = sp_OADestroy @dt
            RETURN
          END

        EXEC sp_OAGetProperty @fileObj, 'Filename', @sTmp0 OUT
        PRINT @sTmp0

        -- Get the last-modified date/time
        EXEC sp_OAGetProperty @fileObj, 'LastModifiedTimeStr', @sTmp0 OUT
        EXEC sp_OAMethod @dt, 'SetFromRfc822', @success OUT, @sTmp0

        -- Get the date/time in other formats offered by the CkDateTime object.

        -- such as Wed, 18 Oct 2017 09:08:21 GMT

        EXEC sp_OAMethod @dt, 'GetAsRfc822', @sTmp0 OUT, @bLocalDateTime
        PRINT 'RFC822 format: ' + @sTmp0

        -- such as 1990-12-31T23:59:60Z

        EXEC sp_OAMethod @dt, 'GetAsTimestamp', @sTmp0 OUT, @bLocalDateTime
        PRINT 'Timestamp: ' + @sTmp0

        -- Such as: "02/16/2008 12:15:12"  where hour is 0 to 23.

        EXEC sp_OAMethod @dt, 'GetAsIso8601', @sTmp0 OUT, 'MM/DD/YYYY hh:mm:ss', @bLocalDateTime
        PRINT 'RFC822 format: ' + @sTmp0

        SELECT @i = @i + 1
      END


    PRINT 'Success.'

    EXEC @hr = sp_OADestroy @sftp
    EXEC @hr = sp_OADestroy @dirListing
    EXEC @hr = sp_OADestroy @fileObj
    EXEC @hr = sp_OADestroy @sbFilename
    EXEC @hr = sp_OADestroy @dt


END
GO