SQL Server
SQL Server
FTP Set Remote File Date/Time Equal to Local File's Last-Modified Date/Time
See more FTP Examples
Demonstrates how to set a remote file's date/time to be equal to a local file's date/time.Important: Not all FTP servers support the ability to set a file's date/time.
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
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example assumes Chilkat Ftp2 to have been previously unlocked.
-- See Unlock Ftp2 for sample code.
DECLARE @ftp int
EXEC @hr = sp_OACreate 'Chilkat.Ftp2', @ftp OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @ftp, 'Hostname', 'www.authtls-ftps-server.com'
EXEC sp_OASetProperty @ftp, 'Username', 'FTP_LOGIN'
EXEC sp_OASetProperty @ftp, 'Password', 'FTP_PASSWORD'
EXEC sp_OASetProperty @ftp, 'AuthTls', 1
EXEC sp_OASetProperty @ftp, 'Port', 21
-- Connect to the FTP server using explicit TLS (AUTH TLS).
EXEC sp_OAMethod @ftp, 'ConnectOnly', @success OUT
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ftp
RETURN
END
-- Authenticate.
EXEC sp_OAMethod @ftp, 'LoginAfterConnectOnly', @success OUT
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @ftp
RETURN
END
-- We're going to get the last-mod date/time for the local file
-- "qa_data/hamlet.xml", and then set the remote "hamlet.xml" to this date/time.
DECLARE @fac int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT
DECLARE @dt int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @dt OUT
DECLARE @lastModTimestamp nvarchar(4000)
EXEC sp_OAMethod @fac, 'GetFileTimeStr', @lastModTimestamp OUT, 'qa_data/hamlet.xml', 0
EXEC sp_OAMethod @dt, 'SetFromTimestamp', @success OUT, @lastModTimestamp
EXEC sp_OAMethod @ftp, 'SetRemoteFileDt', @success OUT, @dt, 'hamlet.xml'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @dt
EXEC @hr = sp_OADestroy @ftp
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @dt
RETURN
END
EXEC sp_OAMethod @ftp, 'Disconnect', @success OUT
PRINT 'Success.'
EXEC @hr = sp_OADestroy @ftp
EXEC @hr = sp_OADestroy @fac
EXEC @hr = sp_OADestroy @dt
END
GO