Sample code for 30+ languages & platforms
SQL Server

Control How FTP Directory Listings Are Requested

See more FTP Examples

Demonstrates the AllowMlsd, PreferNlst, and ListOption properties, which control which listing command Chilkat uses and what options it passes.

Background: FTP has three listing commands with different trade-offs. MLSD is machine-readable and standardized, so AllowMlsd (on by default) prefers it when the server supports it. When it must fall back to the legacy commands, PreferNlst chooses names-only NLST over the fuller LIST, and ListOption appends flags such as -a (to include hidden files) literally to LIST. Together they adapt listing to a particular server's capabilities and conventions.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    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', 'ftp.example.com'
    EXEC sp_OASetProperty @ftp, 'Username', 'myFtpLogin'

    --  AllowMlsd (default 1) requests machine-readable MLSD listings when the server advertises
    --  support.  MLSD is preferred because its format is standardized.
    EXEC sp_OASetProperty @ftp, 'AllowMlsd', 1

    --  PreferNlst (default 0) chooses NLST over LIST when a legacy listing is needed and MLSD is
    --  unavailable.  NLST returns only names.
    EXEC sp_OASetProperty @ftp, 'PreferNlst', 0

    --  ListOption is appended literally to a legacy LIST command; "-a" sends "LIST -a" to include
    --  hidden files on Unix servers.
    EXEC sp_OASetProperty @ftp, 'ListOption', '-a'

    --  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.
    EXEC sp_OASetProperty @ftp, 'Password', 'myPassword'

    EXEC sp_OAMethod @ftp, 'Connect', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        RETURN
      END

    DECLARE @n int
    EXEC sp_OAMethod @ftp, 'GetDirCount', @n OUT

    PRINT 'Entries: ' + @n

    EXEC sp_OAMethod @ftp, 'Disconnect', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        RETURN
      END

    EXEC @hr = sp_OADestroy @ftp


END
GO