FTP Directory Synchronization and Removing Files

Chilkat FTP2 provides methods for synchronizing a local directory tree with a remote directory tree — in both directions.  The SyncLocalTree method downloads files from the remote tree to the local tree, and SyncRemoteTree uploads files from the local tree to the remote tree.  Both have a number of different “modes” that determine which files are transferred, depending on existence, last-modified date comparison, etc.  However, one thing that is intentionally missing is a feature to automatically delete files that do not exist on the other side.  The synchronization is only additive, not deletive.  There is a workaround though, as explained below.

Case #1: Deleting files in the remote tree that do not exist in the local tree.

Determine which files need to be deleted by calling SyncLocalTree using mode #1.  Write an event callback handler for OnBeginDownloadFile (see http://www.chilkatsoft.com/p/p_385.asp ) to skip every download so that nothing is acutally downloaded.  Instead, you’ll build a list of files that would’ve been downloaded because they exist on the server but not locally.  Then write a loop that iterates over the list and calls DeleteRemoteFile for each.

Case #2: Deleting files in the local tree that do not exist in the remote tree.

The same technique is used.  Determinen which files need to be deleted by calling SyncRemoteTree using mode #1.  Write an event callback handler for OnBeginUploadFile to skip every upload so that nothing is actually uploaded.  Instead, you’ll build a list of files that would’ve been uploaded because they exists locally but not on the server.  Then write a loop that iterates over the list can deletes each local file.

Tags :