Announcement

Collapse
No announcement yet.

folder sync (.vbs), how to add: delete differences

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • folder sync (.vbs), how to add: delete differences

    folder sync (vbs script), how to add: delete differences

    I use this vbs script from http://www.808.dk/?code-foldersync to sync 2 folders (the 'one-way sync' version).
    This works like a charm in my opinion, but there's only one thing missing for me and I don't know how to fix this.

    Situation:
    Folder1 is my basic 'master' folder and is always up-2-date
    Folder2 is the mirror folder

    If I use the 'one-way sync' example (as copied below from http://www.808.dk/?code-foldersync) then when I Sync "Folder1"->"Folder2" everything is copied from "Folder1" to "Folder2" as supposed, BUT if there are files or folders removed from "Folder1" they won't get deleted from "Folder2" with this script. Is there a "simple" solution to add this in the current script?

    I hope someone can help me out and hopefully I sketched my situation clearly enough?

    Currently used example script taken from http://www.808.dk/?code-foldersync
    Code:
    Option Explicit
    
    ForceScriptEngine("cscript")
    
    Dim wshArgs
    Set wshArgs = Wscript.Arguments
    If WshArgs.Count = 2 Then
      Call SyncFolders(WshArgs.Item(0), WshArgs.Item(1))
    Else
      Wscript.Echo("Wrong number of arguments. Syntax: SyncFolders Folder1 Folder2")
      Wscript.Sleep(3000) ' To allow Function syntax popup message to be seen.
    End If
    
    Sub SyncFolders(strFolder1, strFolder2)
      ' Note: This version only copies from folder1 to folder2
      Dim objFileSys
      Dim objFolder1
      Dim objFolder2
      Dim objFile1
      Dim objFile2
      Dim objSubFolder
      Dim arrFolders
      Dim i
      arrFolders = Array(strFolder1, strFolder2)
      Set objFileSys = CreateObject("Scripting.FileSystemObject")
      For i = 0 To 1 ' Make sure that missing folders are created first:
        If objFileSys.FolderExists(arrFolders(i)) = False Then
          wscript.echo("Creating folder " & arrFolders(i))
          objFileSys.CreateFolder(arrFolders(i))
        End If
      Next
      Set objFolder1 = objFileSys.GetFolder(strFolder1)
      Set objFolder2 = objFileSys.GetFolder(strFolder2)
      For Each objFile1 in objFolder1.files
        If Not objFileSys.FileExists(objFolder2 & "\" & objFile1.name) Then
          Wscript.Echo("Copying " & objFolder1 & "\" & objFile1.name & _
            " to " & objFolder2 & "\" & objFile1.name)
          objFileSys.CopyFile objFolder1 & "\" & objFile1.name, _
            objFolder2 & "\" & objFile1.name
        Else
          Set objFile2 = objFileSys.GetFile(objFolder2 & "\" & objFile1.name)
          If objFile1.DateLastModified > objFile2.DateLastModified Then
            Wscript.Echo("Overwriting " & objFolder2 & "\" & objFile1.name & _
              " with " & objFolder1 & "\" & objFile1.name)
            objFileSys.CopyFile objFolder1 & "\" & objFile1.name, _
              objFolder2 & "\" & objFile1.name    
          End If
        End If
      Next
      For Each objSubFolder in objFolder1.subFolders
        Call SyncFolders(strFolder1 & "\" & objSubFolder.name, strFolder2 & _
          "\" & objSubFolder.name)
      Next
      Set objFileSys = Nothing
    End Sub
    
    Sub ForceScriptEngine(strScriptEng)
      ' Forces this script to be run under the desired scripting host.
      ' Valid arguments are "wscript" or "cscript".
      ' The command line arguments are passed on to the new call.
      Dim arrArgs
      Dim strArgs
      For Each arrArgs In WScript.Arguments
        strArgs = strArgs & " " & Chr(34) & arrArgs & Chr(34)
      Next
      If Lcase(Right(Wscript.FullName, 12)) = "\wscript.exe" Then
        If Instr(1, Wscript.FullName, strScriptEng, 1) = 0 Then
          CreateObject("Wscript.Shell").Run "cscript.exe //Nologo " & _
            Chr(34) & Wscript.ScriptFullName & Chr(34) & strArgs
          Wscript.Quit
        End If
      Else
        If Instr(1, Wscript.FullName, strScriptEng, 1) = 0 Then
          CreateObject("Wscript.Shell").Run "wscript.exe " & Chr(34) & _
            Wscript.ScriptFullName & Chr(34) & strArgs
          Wscript.Quit
        End If
      End If
    End Sub

  • #2
    Re: folder sync (.vbs), how to add: delete differences

    the other script (copied below) will image both folders (with each other), so I need something in the middle of things ;-p Haven't figured it out yet, hopefully someone can help me out...

    Code:
    Option Explicit
    
    ForceScriptEngine("cscript")
    
    Dim wshArgs
    Set wshArgs = Wscript.Arguments
    If WshArgs.Count = 2 Then
      Call SyncFolders(WshArgs.Item(0), WshArgs.Item(1))
      ' Also run once in reverse to catch mismatching subfolder count:
      Call SyncFolders(WshArgs.Item(1), WshArgs.Item(0))
    Else
      Wscript.Echo("Wrong number of arguments. Syntax: SyncFolders Folder1 Folder2")
      Wscript.Sleep(3000) ' To allow Function syntax popup message to be seen.
    End If
    
    Sub SyncFolders(strFolder1, strFolder2)
      Dim objFileSys
      Dim objFolder1
      Dim objFolder2
      Dim objFile1
      Dim objFile2
      Dim objSubFolder
      Dim arrFolders
      Dim i
      Set objFileSys = CreateObject("Scripting.FileSystemObject")
      arrFolders = Array(strFolder1, strFolder2)
      For i = 0 To 1 ' Make sure that missing folders are created first:
        If objFileSys.FolderExists(arrFolders(i)) = False Then
          wscript.echo("Creating folder " & arrFolders(i))
          objFileSys.CreateFolder(arrFolders(i))
        End If
      Next
      Set objFolder1 = objFileSys.GetFolder(strFolder1)
      Set objFolder2 = objFileSys.GetFolder(strFolder2)
      For i = 0 To 1
        If i = 1 Then ' Reverse direction of file compare in second run
          Set objFolder1 = objFileSys.GetFolder(strFolder2)
          Set objFolder2 = objFileSys.GetFolder(strFolder1)
        End If
        For Each objFile1 in objFolder1.files
          If Not objFileSys.FileExists(objFolder2 & "\" & objFile1.name) Then
            Wscript.Echo("Copying " & objFolder1 & "\" & objFile1.name & _
              " to " & objFolder2 & "\" & objFile1.name)
            objFileSys.CopyFile objFolder1 & "\" & objFile1.name, _
              objFolder2 & "\" & objFile1.name
          Else
            Set objFile2 = objFileSys.GetFile(objFolder2 & "\" & objFile1.name)
            If objFile1.DateLastModified > objFile2.DateLastModified Then
              Wscript.Echo("Overwriting " & objFolder2 & "\" & objFile1.name & _
                " with " & objFolder1 & "\" & objFile1.name)
              objFileSys.CopyFile objFolder1 & "\" & objFile1.name, _
                objFolder2 & "\" & objFile1.name    
            End If
          End If
        Next
      Next
      For Each objSubFolder in objFolder1.subFolders
        Call SyncFolders(strFolder1 & "\" & objSubFolder.name, strFolder2 & _
          "\" & objSubFolder.name)
      Next
      Set objFileSys = Nothing
    End Sub
    
    Sub ForceScriptEngine(strScriptEng)
      ' Forces this script to be run under the desired scripting host.
      ' Valid arguments are "wscript" or "cscript".
      ' The command line arguments are passed on to the new call.
      Dim arrArgs
      Dim strArgs
      For Each arrArgs In WScript.Arguments
        strArgs = strArgs & " " & Chr(34) & arrArgs & Chr(34)
      Next
      If Lcase(Right(Wscript.FullName, 12)) = "\wscript.exe" Then
        If Instr(1, Wscript.FullName, strScriptEng, 1) = 0 Then
          CreateObject("Wscript.Shell").Run "cscript.exe //Nologo " & _
            Chr(34) & Wscript.ScriptFullName & Chr(34) & strArgs
          Wscript.Quit
        End If
      Else
        If Instr(1, Wscript.FullName, strScriptEng, 1) = 0 Then
          CreateObject("Wscript.Shell").Run "wscript.exe " & Chr(34) & _
            Wscript.ScriptFullName & Chr(34) & strArgs
          Wscript.Quit
        End If
      End If
    End Sub
    With kind regards, Tim.

    Comment


    • #3
      Re: folder sync (.vbs), how to add: delete differences

      For the first one, why not delete everything from Folder2 then copy from Folder1 - appreciate it would be more file operations, but would keep them fully synced.
      Tom Jones
      MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
      PhD, MSc, FIAP, MIITT
      IT Trainer / Consultant
      Ossian Ltd
      Scotland

      ** Remember to give credit where credit is due and leave reputation points where appropriate **

      Comment


      • #4
        Re: folder sync (.vbs), how to add: delete differences

        Originally posted by Ossian View Post
        For the first one, why not delete everything from Folder2 then copy from Folder1 - appreciate it would be more file operations, but would keep them fully synced.
        that could be an option indeed, but only delete everything in folder2 when there's a change, how could/should I incorporate this?

        Comment


        • #5
          Re: folder sync (.vbs), how to add: delete differences

          Compare folder total size first?
          I suppose there is a faint chance two changes might leave the size unchanged though
          Alternatively see if any folder date stamps change when files are modified
          Tom Jones
          MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
          PhD, MSc, FIAP, MIITT
          IT Trainer / Consultant
          Ossian Ltd
          Scotland

          ** Remember to give credit where credit is due and leave reputation points where appropriate **

          Comment


          • #6
            Re: folder sync (.vbs), how to add: delete differences

            Perhaps combine these two options?
            -> check folder size and check folder date stamp if any changes delete everything in folder2, but how would one incorporate that in this script the right way?

            Code:
            Option Explicit
            
            ForceScriptEngine("cscript")
            
            Dim wshArgs
            Set wshArgs = Wscript.Arguments
            If WshArgs.Count = 2 Then
              Call SyncFolders(WshArgs.Item(0), WshArgs.Item(1))
              ' Also run once in reverse to catch mismatching subfolder count:
              Call SyncFolders(WshArgs.Item(1), WshArgs.Item(0))
            Else
              Wscript.Echo("Wrong number of arguments. Syntax: SyncFolders Folder1 Folder2")
              Wscript.Sleep(3000) ' To allow Function syntax popup message to be seen.
            End If
            
            Sub SyncFolders(strFolder1, strFolder2)
              Dim objFileSys
              Dim objFolder1
              Dim objFolder2
              Dim objFile1
              Dim objFile2
              Dim objSubFolder
              Dim arrFolders
              Dim i
              Set objFileSys = CreateObject("Scripting.FileSystemObject")
              arrFolders = Array(strFolder1, strFolder2)
              For i = 0 To 1 ' Make sure that missing folders are created first:
                If objFileSys.FolderExists(arrFolders(i)) = False Then
                  wscript.echo("Creating folder " & arrFolders(i))
                  objFileSys.CreateFolder(arrFolders(i))
                End If
              Next
              Set objFolder1 = objFileSys.GetFolder(strFolder1)
              Set objFolder2 = objFileSys.GetFolder(strFolder2)
              For i = 0 To 1
                If i = 1 Then ' Reverse direction of file compare in second run
                  Set objFolder1 = objFileSys.GetFolder(strFolder2)
                  Set objFolder2 = objFileSys.GetFolder(strFolder1)
                End If
                For Each objFile1 in objFolder1.files
                  If Not objFileSys.FileExists(objFolder2 & "\" & objFile1.name) Then
                    Wscript.Echo("Copying " & objFolder1 & "\" & objFile1.name & _
                      " to " & objFolder2 & "\" & objFile1.name)
                    objFileSys.CopyFile objFolder1 & "\" & objFile1.name, _
                      objFolder2 & "\" & objFile1.name
                  Else
                    Set objFile2 = objFileSys.GetFile(objFolder2 & "\" & objFile1.name)
                    If objFile1.DateLastModified > objFile2.DateLastModified Then
                      Wscript.Echo("Overwriting " & objFolder2 & "\" & objFile1.name & _
                        " with " & objFolder1 & "\" & objFile1.name)
                      objFileSys.CopyFile objFolder1 & "\" & objFile1.name, _
                        objFolder2 & "\" & objFile1.name    
                    End If
                  End If
                Next
              Next
              For Each objSubFolder in objFolder1.subFolders
                Call SyncFolders(strFolder1 & "\" & objSubFolder.name, strFolder2 & _
                  "\" & objSubFolder.name)
              Next
              Set objFileSys = Nothing
            End Sub
            
            Sub ForceScriptEngine(strScriptEng)
              ' Forces this script to be run under the desired scripting host.
              ' Valid arguments are "wscript" or "cscript".
              ' The command line arguments are passed on to the new call.
              Dim arrArgs
              Dim strArgs
              For Each arrArgs In WScript.Arguments
                strArgs = strArgs & " " & Chr(34) & arrArgs & Chr(34)
              Next
              If Lcase(Right(Wscript.FullName, 12)) = "\wscript.exe" Then
                If Instr(1, Wscript.FullName, strScriptEng, 1) = 0 Then
                  CreateObject("Wscript.Shell").Run "cscript.exe //Nologo " & _
                    Chr(34) & Wscript.ScriptFullName & Chr(34) & strArgs
                  Wscript.Quit
                End If
              Else
                If Instr(1, Wscript.FullName, strScriptEng, 1) = 0 Then
                  CreateObject("Wscript.Shell").Run "wscript.exe " & Chr(34) & _
                    Wscript.ScriptFullName & Chr(34) & strArgs
                  Wscript.Quit
                End If
              End If
            End Sub

            Comment


            • #7
              Re: folder sync (.vbs), how to add: delete differences

              Still trying to figure something out ;-p

              Would be cool(est) if it compared if something was removed in #1 and then also remove it from #2 (since #1 is the "master")

              Comment


              • #8
                Re: folder sync (.vbs), how to add: delete differences

                maybe something like this,
                This script first compares the destination folder with the source folder (sub EnumDestinationDir), deleting everything from the destination directory that could not be found in the source directory.
                Then it compares the source folder with the destination folder (sub EnumSourceDir), copies everything from the source directory that does not exist or is of a different version in the destination directory to the destination directory.

                Code:
                ' command line to start this script:
                ' cmd.exe /c cscript.exe /nologo folder-sync.vbs "D:\Documents" "\\server.domain.local\staging$\backup" >> folder-sync.log 2>&1
                
                
                Dim FSO
                Set FSO = CreateObject("Scripting.FileSystemObject")
                
                Set wshArgs = Wscript.Arguments
                If WshArgs.Count = 2 Then
                   strBase1 = trim(WshArgs.Item(0))
                   strBase2 = trim(WshArgs.Item(1))
                Else
                   Set wshShell = WScript.CreateObject("WScript.Shell")
                   WshShell.Popup "Wrong number of arguments. Syntax: SyncFolders ""source Folder"" ""destination Folder""", 3
                   wscript.quit
                End If
                
                          '##---------------------------
                           wscript.echo now()
                          '##---------------------------
                
                EnumDestinationDir FSO.GetFolder(strBase2)
                EnumSourceDir FSO.GetFolder(strBase1)
                
                          '##---------------------------
                           wscript.echo ""
                          '##---------------------------
                
                Sub EnumDestinationDir(ByVal Folder2)
                   CompareWithSource Folder2.Path
                   On Error Resume Next
                   For Each Subfolder2 in Folder2.SubFolders
                     EnumDestinationDir Subfolder2
                   Next
                End Sub
                    '''
                    Sub CompareWithSource(sPath2)
                      On Error Resume Next
                      Set objFolder2 = FSO.GetFolder(sPath2)
                      strDestSubFolder = Replace(objFolder2.path, strBase2, "", 1, -1, vbTextCompare)
                      If FSO.FolderExists(strBase1 & strDestSubFolder) = False Then
                          '##---------------------------
                           wscript.echo "Removed directory from backup folder: " & objFolder2.path
                           objFolder2.Delete True
                          '##---------------------------
                      End IF
                      err.clear
                      Set objFolder2 = FSO.GetFolder(sPath2)
                      If err.number <> 0 Then exit sub
                      Set colFiles = objFolder2.Files
                      For Each objFile2 in colFiles
                        strDestFile = Replace(objFile2.Path, strBase2, "", 1, -1, vbTextCompare)
                        If NOT FSO.FileExists(strBase1 & strDestFile) Then
                          '##---------------------------
                           wscript.echo "Removed file from backup folder: " & objFile2.path
                           objFile2.Delete True
                          '##---------------------------
                        End IF
                      Next
                    End Sub
                
                Sub EnumSourceDir(ByVal Folder1)
                   CompareWithDestination Folder1.Path
                   On Error Resume Next
                   For Each Subfolder1 in Folder1.SubFolders
                     EnumSourceDir Subfolder1
                   Next
                End Sub
                    '''
                    Sub CompareWithDestination(sPath1)
                      On Error Resume Next
                      Set objFolder1 = FSO.GetFolder(sPath1)
                      strSrceSubFolder = Replace(objFolder1.path, strBase1, "", 1, -1, vbTextCompare)
                      If FSO.FolderExists(strBase2 & strSrceSubFolder) = False Then
                          '##---------------------------
                           wscript.echo "Backed up new directory: " & objFolder1.path
                           FSO.CreateFolder strBase2 & strSrceSubFolder
                           FSO.CopyFolder objFolder1.path & "\*", strBase2 & strSrceSubFolder
                          '##---------------------------
                      End If
                      err.clear
                      Set colFiles = objFolder1.Files
                      For Each objFile1 in colFiles
                        strSrceFile = Replace(objFile1.Path, strBase1, "", 1, -1, vbTextCompare)
                        If FSO.FileExists(strBase2 & strSrceFile) = True Then
                          Set objFile2 = FSO.GetFile(strBase2 & strSrceFile)
                          IF DateDiff("s",objFile1.DateLastModified, objFile2.DateLastModified) <> 0 Then
                          '##---------------------------
                           wscript.echo "Backed up changed file: " & objFile1.path
                           FSO.CopyFile objFile1.path, strBase2 & strSrceSubFolder & "\", True
                          '##---------------------------
                          End If
                        Else
                          '##---------------------------
                           wscript.echo "Backed up new file: " & objFile1.path
                           FSO.CopyFile objFile1.path, strBase2 & strSrceSubFolder & "\"
                          '##---------------------------
                        End IF
                      Next
                    End Sub
                b.t.w. I just found a similar script here: http://www.tek-tips.com/viewthread.cfm?qid=1685270


                But why not simply invoke a Robocopy command? I does everything you need and it may even run faster.

                /Rems
                Last edited by Rems; 20th March 2014, 19:11.

                This posting is provided "AS IS" with no warranties, and confers no rights.

                __________________

                ** Remember to give credit where credit's due **
                and leave Reputation Points for meaningful posts

                Comment


                • #9
                  Re: folder sync (.vbs), how to add: delete differences

                  COOL Thx

                  just by choice, so there wasn't any need for an extra program and have access to vbs by default...Perhaps Robocopy is smarter (more usefull) because of it's speed indeed OR maybe Rsync (or cwRsync) instead?

                  Like your script though, choices, choices...

                  Comment


                  • #10
                    Re: folder sync (.vbs), how to add: delete differences

                    im getting message :

                    Wrong number of arguments.

                    what arguments need to passed and how?

                    Comment


                    • #11
                      Re: folder sync (.vbs), how to add: delete differences

                      Originally posted by mohitaroraiec View Post
                      im getting message :

                      Wrong number of arguments.

                      what arguments need to passed and how?
                      Two arguments must be provided on the command line.

                      The first is the source folder (full path and the string must be wrapped in doublequote signs)
                      Second argument is the destination folder (this path also wrapped in doublequote signs)

                      /Rems

                      This posting is provided "AS IS" with no warranties, and confers no rights.

                      __________________

                      ** Remember to give credit where credit's due **
                      and leave Reputation Points for meaningful posts

                      Comment

                      Working...
                      X