SQL Server
SQL Server
Get a Remote File's Last-Access Time
See more SFTP Examples
Demonstrates the Chilkat SFtp.GetFileLastAccessStr method, which returns a remote file's last-access date and time. The arguments are the remote path (or handle), whether symbolic links are followed, and whether the first argument is a handle.
Background: The access time is meant to update whenever a file is read, but in practice it is often unreliable: many systems mount filesystems with options like
noatime to avoid a disk write on every read, so the value may not reflect recent reads. Treat it as a hint rather than an authoritative record of when a file was last opened.Chilkat SQL Server Downloads
-- 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
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Demonstrates the SFtp.GetFileLastAccessStr method, which returns a remote file's last-access
-- date and time. The 1st argument is the remote path (or handle), the 2nd selects whether
-- symbolic links are followed, and the 3rd indicates whether the 1st argument is a handle.
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
-- The 2nd argument (bFollowLinks) selects whether a symbolic link is followed to its target.
-- The 3rd argument (bIsHandle) is 0 here because the 1st argument is a path, not an
-- open handle.
DECLARE @bFollowLinks int
SELECT @bFollowLinks = 1
DECLARE @bIsHandle int
SELECT @bIsHandle = 0
DECLARE @lastAccess nvarchar(4000)
EXEC sp_OAMethod @sftp, 'GetFileLastAccessStr', @lastAccess OUT, 'subdir/report.pdf', @bFollowLinks, @bIsHandle
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
PRINT 'Last accessed: ' + @lastAccess
EXEC sp_OAMethod @sftp, 'Disconnect', NULL
EXEC @hr = sp_OADestroy @sftp
END
GO