edtFTPnet/PRO - Secure FTP component for .NET | Free Trial | Pricing

How to synchronize directories

Directory synchronization aims to ensure that the files in a directory on the local machine match those in a directory on the remote machine.

This means that instead of uploading an entire directory, with a single command only the missing or changed files can be uploaded, saving greatly on time and bandwidth. This is a common requirement in batch jobs.

These capabilities are in ExFTPConnection and SecureFTPConnection via a method called Synchronize:

public void Synchronize( string localDirectory, string serverDirectory, FTPSyncRules syncRules)
public void Synchronize( string localDirectory, string serverDirectory)

The key class is FTPSyncRules, which controls how the files are matched. An instance of this class is either supplied as a method parameter, or the default instance is used. The default instance is accessible via the DefaultSyncRules property on the connection.

The rules encapsulated in FTPSyncRules are quite comprehensive, but if they are insufficient the SynchronizingFile event may be used to provide customized logic for matching files.

The properties of FTPSyncRules are listed and discussed below:

Property
Description
Direction
Controls the direction of synchronization. For UPLOAD, the source is the local file system. For DOWNLOAD, the source is the remote file system.
IncludeFilesInFilter
Controls whether the filter is an inclusion filter or an exclusion filter. If this flag is true (default), then only the files that match the filter will be synchronized. If false, only the non-matching files will be synchronized.
FilterType
Controls whether the FileNameFilter should be interpreted as a wildcard string or as a regular expression. Wildcard strings use '*' to match multiple characters and "?" to match any single character.
FileNameFilter
File-name filter that controls which files are synchronized. It may contain multiple strings separated by commas. Each string may be a Regex expression or a wildcard string. The FilterType property determines the type of filter - wildcard or regex. The IncludeFilesInFilter property is used to use the filter to include or exclude files.
IgnoreDate
If set (false by default) dates are ignored when performing file comparisons.
TransferNewOnly
If set (false by default) then only files that do not exist on the target file-system will be considered.
TransferMatchingOnly
If set (false by default) then only files that exist both on the source and target file-systems will be considered.
DeleteIfSourceAbsent
If set (false by default) then if a file exists on the target but not on the source then that file will be deleted.
SyncLocalDateAfterDownload
If set (false by default) this flag will cause the control to set the date of the local file to be the same as the remote file after the file has been downloaded.  This property only has an effect when real-time mode is enabled.
IncludeSubdirectories
If set (true by default) this flag will cause files in subdirectories to be included in the synchronization operation.

Using the default FTPSyncRules instance synchronizes a local source directory with a remote directory. Dates are used in file comparisons, it is a recursive synchronization, and no filters are set. The result will be that all files in the local directory that are missing or out of date in the remote directory will be uploaded.

Date matching

As noted, the IgnoreDate property is false by default, meaning that dates are checked when synchronizing files. If the target has an older version of a file, the newer version from the source is used to overwrite it.

It should be noted, however, that very often FTP servers are located in different time zones to their clients. It is also common for FTP servers to list file stamps in GMT.

If this is the case, the TimeDifference property (on FTPConnection and its subclasses) should be used to ensure correct date matching. For example, if the FTP server is 5 hours behind that of the client, this property should be set to -5 hours.