Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
|
FTP XML Directory Listing / Download Most Recently Modified FilesDemonstrates how to retrieve a directory listing in XML format, sort it by last-modified date, and download the 5 most recently modified files. Note: This example relies upon a new feature added to the GetXmlDirListing method. Namely, the resultant XML includes a "full" attribute with a date/time that is in a sortable format. This feature is available in the pre-release, or in new versions released after 20-May-2008.
CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @sTmp0 nvarchar(4000) DECLARE @ftp int EXEC @hr = sp_OACreate 'Chilkat.Ftp2', @ftp OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int -- Any string unlocks the component for the 1st 30-days. EXEC sp_OAMethod @ftp, 'UnlockComponent', @success OUT, 'Anything for 30-day trial' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END EXEC sp_OASetProperty @ftp, 'Hostname', 'ftp.chilkatsoft.com' EXEC sp_OASetProperty @ftp, 'Username', 'myLogin' EXEC sp_OASetProperty @ftp, 'Password', 'myPassword' -- Connect and login to the FTP server. EXEC sp_OAMethod @ftp, 'Connect', @success OUT IF @success <> 1 BEGIN EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END -- Recursively download the listings for the complete -- remote directory tree at the current remote dir. -- In this case, we haven't called ChangeRemoteDir, -- so the current remote dir is the root directory of the -- FTP account. DECLARE @strXml nvarchar(4000) EXEC sp_OAMethod @ftp, 'GetXmlDirListing', @strXml OUT, '*.asp' IF @strXml Is NULL BEGIN EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END -- Load the XML directory listing into a Chilkat XML object: DECLARE @xml int EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END EXEC sp_OAMethod @xml, 'LoadXml', NULL, @strXml -- Sort by a record attribute: DECLARE @ascending int SELECT @ascending = 0 EXEC sp_OAMethod @xml, 'SortRecordsByAttribute', NULL, 'lastModTime', 'full', @ascending -- Download the 5 most recent files: DECLARE @n int EXEC sp_OAGetProperty @xml, 'NumChildren', @n OUT IF @n > 5 BEGIN SELECT @n = 5 END DECLARE @child int DECLARE @filename nvarchar(4000) DECLARE @i int SELECT @i = 0 WHILE @i <= @n - 1 BEGIN EXEC sp_OAMethod @xml, 'GetChild', @child OUT, @i EXEC sp_OAMethod @child, 'GetChildContent', @filename OUT, 'name' PRINT @filename EXEC sp_OAMethod @ftp, 'GetFile', @success OUT, @filename, @filename IF @success <> 1 BEGIN EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 BREAK END SELECT @i = @i + 1 END -- The full sorted XML may be obtained like this: EXEC sp_OAMethod @xml, 'GetXml', @strXml OUT EXEC sp_OAMethod @ftp, 'Disconnect', NULL END GO |
© 2000-2010 Chilkat Software, Inc. All Rights Reserved.