Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) SFTP Get File Date/Times in Different FormatsSee more SFTP ExamplesDemonstrates how to get remote file date/times in different formats.
-- 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) -- This requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @sftp int -- Use "Chilkat_9_5_0.SFtp" for versions of Chilkat < 10.0.0 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 DECLARE @success int EXEC sp_OAMethod @sftp, 'Connect', @success OUT, @hostname, @port IF @success <> 1 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 <> 1 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 <> 1 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 <> 1 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 sp_OAMethod @sftp, 'ReadDir', @dirListing OUT, @handle EXEC sp_OAGetProperty @sftp, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @sftp RETURN END -- Close the directory handle EXEC sp_OAMethod @sftp, 'CloseHandle', @success OUT, @handle IF @success <> 1 BEGIN EXEC sp_OAGetProperty @sftp, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @sftp 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 @i int SELECT @i = 0 DECLARE @n int EXEC sp_OAGetProperty @dirListing, 'NumFilesAndDirs', @n OUT DECLARE @sbFilename int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbFilename OUT DECLARE @caseSensitive int SELECT @caseSensitive = 0 DECLARE @bLocalDateTime int SELECT @bLocalDateTime = 0 WHILE @i < @n BEGIN DECLARE @fileObj int EXEC sp_OAMethod @dirListing, 'GetFileObject', @fileObj OUT, @i EXEC sp_OAGetProperty @fileObj, 'Filename', @sTmp0 OUT PRINT @sTmp0 -- Get the last-modified date/time DECLARE @dt int EXEC sp_OAMethod @fileObj, 'GetLastModifiedDt', @dt OUT -- Get the date/time in any format 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 EXEC @hr = sp_OADestroy @dt EXEC @hr = sp_OADestroy @fileObj SELECT @i = @i + 1 END EXEC @hr = sp_OADestroy @dirListing PRINT 'Success.' EXEC @hr = sp_OADestroy @sftp EXEC @hr = sp_OADestroy @sbFilename END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.