locked
Syncronizing two folders RRS feed

  • Question

  • I'm trying to build a program which will synchronize two folders.
    All i want is to upload the files from folder a to folder b, delete the unnecessary files from folder b and replace the modified files.
    With the code below, I've managed to upload the files.
    How can i "sync with delete"?
    I've read that i must use "echo" but i couldn't find anything like that.
    Thanks in advance


    Code Snippet

    Imports Microsoft.Synchronization
    Imports Microsoft.Synchronization.Files
    Imports System.IO
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim _sourceDir As String = "c:\temp\a"
    Dim _destDir As String = "c:\temp\b"
    Dim _sourceId As Guid = Guid.NewGuid()
    Dim _destId As Guid = Guid.NewGuid()

    Dim _syncFilter As FileSyncScopeFilter = New FileSyncScopeFilter()
    _syncFilter.AttributeExcludeMask = FileAttributes.Hidden Or FileAttributes.System
    Dim _syncOptions As FileSyncOptions = FileSyncOptions.None
    Using _sourceProvider As FileSyncProvider = New FileSyncProvider(_sourceId, _sourceDir, _syncFilter, _syncOptions)
    Using _destinationProvider As FileSyncProvider = New FileSyncProvider(_destId, _destDir, _syncFilter, _syncOptions)

    Dim _syncAgent As SyncOrchestrator = New SyncOrchestrator()
    _syncAgent.LocalProvider = _sourceProvider
    _syncAgent.RemoteProvider = _destinationProvider
    _syncAgent.Direction = SyncDirectionOrder.Upload
    _syncAgent.Synchronize()
    End Using
    End Using
    End If
    End Sub

    Wednesday, October 22, 2008 10:07 PM

Answers

  • When test.zip goes from A to B you are pretty much done right. You can

    remove the test.rar separately outside of sync. I assume that test.rar is not present on A and hence delete need not be sent to A. Am I missing anything then?

     

    Now if you want the 1MB test.zip to replace the 2MB file on B, then touch the 1MB file to make it look like more recent.

    And I assume that you have these 2 different files in A and B before the start of sync.

    Friday, October 31, 2008 4:56 PM

All replies

  • Firstly to begin with, are your folders A and B in sync? or are you starting with empty B and A has all the files?

     

    From your code, what you did is a 1 way sync. You would need to do another sync the other way around - B to A to bring data from B to A. You can do this either with 2 one way sync or 1 sync that does upload+download or download+upload.

     

    As in (second one way sync)

    _syncAgent.LocalProvider = _destinationProvider

    _syncAgent.RemoteProvider = _sourceProvider
    _syncAgent.Direction = SyncDirectionOrder.Upload
    _syncAgent.Synchronize()

     

    OR:

    SyncDirectionOrder.UploadAndDownload

    OR

    SyncDirectionOrder.DownloadAndUpload

    Tuesday, October 28, 2008 7:40 PM
  • I'm trying to use one-way synchronization.
    I just want to
    delete files that don't exist on the sending side. (like using rsync --delete from linux)
    http://linux.about.com/library/cmd/blcmdl1_rsync.htm

    Let me give you an example.
    I have folder A which contains test.zip and a folder B which contains test.rar.
    I want test.zip from folder A go to folder B and test.rar from folder B removed.

    I also want to check if the files have different sizes.
    Example:
    I have folder A which contains test.zip (1MB size) and a folder B which contains test.zip (2MB size).
    I want test.zip from folder A replace the other from folder B.

    Thursday, October 30, 2008 11:03 PM
  • When test.zip goes from A to B you are pretty much done right. You can

    remove the test.rar separately outside of sync. I assume that test.rar is not present on A and hence delete need not be sent to A. Am I missing anything then?

     

    Now if you want the 1MB test.zip to replace the 2MB file on B, then touch the 1MB file to make it look like more recent.

    And I assume that you have these 2 different files in A and B before the start of sync.

    Friday, October 31, 2008 4:56 PM
  • I have the same requirements as Peoman. 

    1) Copy files from folder A that do not exist in folder B
    2) Update files in folder B with changed files in folder A
    3) Delete anything in folder B that does not exist in folder A

    To suggest that he can delete files outside of Sync is not helpful - if it is necessary to detect files that need deleting after using Sync then why not just implement the Sync function too.

    As it happens, I have got Sync working to the point where it will do the three things listed above.  However, if for example, once the folders are in sync, I delete the contents of folder B - and then run Sync again, it will not detect the changes in folder B and therefore folder B will remain empty.

    The only way I can get it to detect the changes is if I manually delete the metadata file for folder B.  This is madness! - What am I missing here?
    Sunday, February 8, 2009 10:46 PM
  • I had the same problem, and after further investigation it appears to be the GUIDs that you use.

    Try creating the FileSyncProvider with the same guid each time, rather than having a new one every time you run the program. I'm still trying to work out a way to read these ids from the metafiles...
    Wednesday, February 11, 2009 7:47 PM
  • Actually, since you mentioned Meta files.  I was thinking about how to store the GUID outside of the folder store.  The method I ultimately have decided to use involves saving the Guid as part of the MySettings metabase of the application itself.

    Public Shared Function GetReplicaId(ByVal settingsGuid As String, ByRef sysSettings As Object) As SyncId
        Dim replicaId As SyncId = Nothing

        If
    Not String.IsNullOrEmpty(setingsGuid) Then
            If Not (settingsGuid = "00000000-0000-0000-0000-000000000000") Then
                replicaId = New SyncId(New Guid(settingsGuid))
            End If
        End If
        
        If
    replicaId Is Nothing Then
            replicaId = New SyncId(Guid.NewGuid())
            sysSettings = (replicaId.GetGuidId())
        End If

        Return replicaId
    End Function

    The calling code is then formatted as such:

    Dim replica1Id As SyncId = GetReplicaId(My.Settings.srcGuid.ToString, My.Settings.srcGuid)
    Dim replica2Id As SyncId = GetReplicaId(My.Settings.tgtGuid.ToString, My.Settings.tgtGuid)

    Let me know what you think.

    -saige-

    Sunday, March 1, 2009 5:44 AM