Sample code for 30+ languages & platforms
SQL Server

Execute an FTP Upload Plan

See more FTP Examples

Demonstrates the Chilkat Ftp2.PutPlan method, which executes a previously created upload plan. The first argument is the plan text and the second is the local path of a completion log that records each successful operation.

Note: The local paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: The completion log is what makes plan execution resumable: each successful upload is recorded, so if a run is interrupted, re-executing the same plan skips the operations already logged and continues where it left off — invaluable for a large transfer over a flaky connection. The plan's stored absolute remote paths are used regardless of the current remote directory, so execution is independent of session state.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the Ftp2.PutPlan method, which executes a previously created upload plan.  The 1st
    --  argument is the plan text and the 2nd is the local path of a completion log.

    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'

    --  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

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

    --  Load a plan that was previously created with CreatePlan.
    DECLARE @sbPlan int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbPlan OUT

    EXEC sp_OAMethod @sbPlan, 'LoadFile', @success OUT, 'qa_output/upload_plan.txt', 'utf-8'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @sbPlan, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        EXEC @hr = sp_OADestroy @sbPlan
        RETURN
      END
    DECLARE @plan nvarchar(4000)
    EXEC sp_OAMethod @sbPlan, 'GetAsString', @plan OUT

    --  Execute the plan.  Each successful operation is recorded in the completion log, which lets an
    --  interrupted run be resumed without repeating completed uploads.  The plan uses its stored
    --  absolute remote paths even if the current remote directory has changed.
    EXEC sp_OAMethod @ftp, 'PutPlan', @success OUT, @plan, 'qa_output/upload_done.log'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        EXEC @hr = sp_OADestroy @sbPlan
        RETURN
      END

    PRINT 'Upload plan executed.'

    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
        EXEC @hr = sp_OADestroy @sbPlan
        RETURN
      END

    EXEC @hr = sp_OADestroy @ftp
    EXEC @hr = sp_OADestroy @sbPlan


END
GO