Folder Synchronizer Class

Overview

If you want to synchronise files in two directories, there are no system commands available to copy only those files with a later date/time stamp from one directory to another. This class enables you to create an object that can carry out the following, depending on the options that you set at run time:

Property Type Meaning
FromFolder String Folder from which files are to be copied (source)
ToFolder String Folder to which files are to be copied (destination)
CopyIfMissing Boolean Copy the file from the source if there is no matching file in the destination. If this is not set, then synchronisation applies only to files that are in both the source and destination.
OverwriteAll Boolean Copy from source to destination even if the destination file is more recent than the source file
DeleteEmptyFiles Boolean Deletes files from source and destination if the length of the source file = 0
IncludeSubFolders Boolean Will recursively synchronise all sub-folders of the source folder
CreateSubFolders Boolean If IncludeSubFolders is set, then setting this will automatically recursively create all subfolders of the source folder in the destination, if not already there, and copy files from source to destination folders. If this is not set, only those subfolders that are present in both source and destination folders are synchronized.
ReportGrid MSFlexGrid If set to an MSFlexGrid control, then all the actions taken by the object are reported in the grid as a list.
IncludeNormalFiles Boolean Includes files that do not have the System attribute set
IncludeSystemFiles Boolean Includes files with the system attribute
IncludeHiddenFiles Boolean Includes files with the hidden attribute
IncludeArchiveFilesOnly Boolean Only files with the Archive attribute set are included
ClearArchiveFlag Boolean If set, the Archive attribute is reset on files in the From folder after copying.
Attributes String Sets the IncludeXXX flags above by using a string containing any combination of the letters N, S, H and A. So using

.Attributes = "SH"

is equivalent to setting .IncludeSystemFiles and .IncludeHiddenFiles and resetting the others.

Usage and operation

To use this class  include the syncDir.cls class source file in your project. In your application code, create a variable of type SyncDir:

dim [WithEvents] FolderSync as SyncDir

The WithEvents option should be used if you want to trap the Action events (see below).

Create a new object with:

set FolderSync = new SyncDir

The SyncDir class uses the SortedList.cls class, which you must also include in your project.

Action events

When synchronising two folders, the class generates Action events that report the actions taken while synchronising. This is useful for audit trailing.

_Action(ActionType As String, Msg As String)

Action type indicates what information is contained in the Msg:

Action Type Msg
CFG Configuration settings - designed for reading if printed line-by-line
FROM Path of From folder (top level)
TO Path of To folder (top level)
NEWSF Path of subfolder being created in the To folder. Will arise only if createSubFolders option is set.
SUBF Path of subfolder being synchronised. Subsequent action messages apply to files in this subfolder. Will arise only if IncludeSubFolders option is set.
NEW Name of file being copied to TO folder, when no previous version is there.
REPL Name of file being copied to TO folder, to replace previous version
ERR Error message - applies to the preceding action line
SYS System message

You might also like...

Comments

 John

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook