Sample code for 30+ languages & platforms
SQL Server

Sync a Local Tree Up with Preview and Traversal Control

See more FTP Examples

Demonstrates the Chilkat Ftp2.SyncRemoteTree2 method, which uploads a local tree to the current remote directory with extra controls. The arguments are the local root, the upload mode, whether to descend into subdirectories, and whether to run preview-only. In preview mode the results are available via the SyncPreview property.

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 preview flag is what makes this valuable: run it first to see exactly which files would be uploaded — without transferring anything — then run it again for real once the plan looks right. That dry-run step is a strong safeguard before a large or destructive-looking sync. The descend flag lets you limit the operation to the top level, and the mode is the same set of upload rules as SyncRemoteTree.

Chilkat SQL Server Downloads

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

    --  Demonstrates the Ftp2.SyncRemoteTree2 method, which uploads a local tree to the current remote
    --  directory with extra traversal and preview controls.  The 1st argument is the local root, the
    --  2nd is the upload mode, the 3rd selects whether to descend into subdirectories, and the 4th
    --  selects preview-only (no actual transfers).

    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

    --    0 = all selected files
    --    1 = files missing on the server
    --    2 = missing, or newer locally
    --    3 = only files that exist remotely and are newer locally
    --    4 = missing, or a different size
    --    5 = missing, different size, or newer locally

    DECLARE @mode int
    SELECT @mode = 5

    --  Descend into subdirectories.
    DECLARE @bDescend int
    SELECT @bDescend = 1

    --  Preview only: determine what WOULD be uploaded without transferring anything.  The results are
    --  available via the SyncPreview property.
    DECLARE @bPreviewOnly int
    SELECT @bPreviewOnly = 1

    EXEC sp_OAMethod @ftp, 'SyncRemoteTree2', @success OUT, 'qa_data/site', @mode, @bDescend, @bPreviewOnly
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        RETURN
      END

    --  SyncPreview lists the files that would be uploaded.
    EXEC sp_OAGetProperty @ftp, 'SyncPreview', @sTmp0 OUT
    PRINT @sTmp0

    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