Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: batch file to synchronize the contents of two folders  (Read 33363 times)

0 Members and 1 Guest are viewing this topic.

peer

  • Guest
batch file to synchronize the contents of two folders
« on: December 17, 2010, 02:32:38 AM »
one of my tasks is to update files in a folder that is on many computers. can I get help to make a batch file that runs on the client computer to check the contents of a folder by comparing it with the master folder that is in the "server computer". and if there are changes in the size of the file or there is a new file in the master folder, it will automatically be copied when the batch file is executed.
thanks a lot

polle123



    Rookie

    • Experience: Beginner
    • OS: Unknown
    Re: batch file to synchronize the contents of two folders
    « Reply #1 on: December 17, 2010, 09:22:19 AM »
    that should be easy, but it gets more tricky if you would also need to delete files from the mirrors that are no longer on the original copy

    reddevilggg



      Expert

      Thanked: 69
    • Experience: Beginner
    • OS: Windows 7
    Re: batch file to synchronize the contents of two folders
    « Reply #2 on: December 17, 2010, 09:40:08 AM »

    that should be easy,

    Tell us how then!!
    11 cheers for binary !

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: batch file to synchronize the contents of two folders
    « Reply #3 on: December 17, 2010, 11:57:23 AM »
    Quote
    one of my tasks is to update files in a folder that is on many computers. can I get help to make a batch file that runs on the client computer
    It can be done in batch.
    But may I ask why batch is the choice.
    There are other methods often used.

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: batch file to synchronize the contents of two folders
    « Reply #4 on: December 18, 2010, 07:51:07 AM »
    Writing a batch file for this would border on the sadistic.

    Written for XP, my personal favorite is SyncToy which is *censored* near idiot proof.  ;D

    An alternative might be the RoboCopy GUI. The GUI makes it easier than using the command line version which can be quite challenging.

    Also try Google. A quick search turned up many scripts written in VBScript and Powershell.

    Good luck.  8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    donald99



      Rookie
    • Hello
    • Thanked: 2
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Batch file to synchronize the contents of two folders
      « Reply #5 on: December 18, 2010, 10:09:52 AM »

      http://www.tech-pro.net/howto_033.html


      "To use the batch file, open a command prompt and type SYNC followed by the paths of the two folders you want to synchronize, each in quotes. If you want to synchronize subfolders as well, add /S to the command line before pressing Enter. For example, suppose your project is kept in a folder called "My Project" on both your local PC and one with a network name of "DELL". To synchronize this folder, including any subfolders, type the command:

      SYNC "C:\My Project" "\\DELL\My Project" /SWe recommend that you test this on something unimportant before trying it on valuable work files. Note that the two-line batch file has no "idiot-proofing", so it will happily try to synchronize entire hard disks if you tell it to! This method works, but it gets tiresome having to type in the paths of the two folders."

      p.s. I did not test above method. Donald99
      Try

      polle123



        Rookie

        • Experience: Beginner
        • OS: Unknown
        Re: batch file to synchronize the contents of two folders
        « Reply #6 on: December 18, 2010, 10:40:15 AM »
        http://www.tech-pro.net/howto_033.html


        "To use the batch file, open a command prompt and type SYNC followed by the paths of the two folders you want to synchronize, each in quotes. If you want to synchronize subfolders as well, add /S to the command line before pressing Enter. For example, suppose your project is kept in a folder called "My Project" on both your local PC and one with a network name of "DELL". To synchronize this folder, including any subfolders, type the command:

        SYNC "C:\My Project" "\\DELL\My Project" /SWe recommend that you test this on something unimportant before trying it on valuable work files. Note that the two-line batch file has no "idiot-proofing", so it will happily try to synchronize entire hard disks if you tell it to! This method works, but it gets tiresome having to type in the paths of the two folders."

        p.s. I did not test above method. Donald99


        no, there is no such thing as a sync command
        you ripped that out of it's context
        they MADE a sync command that did this:
        Code: [Select]
        XCOPY "%1" "%2" /D /I %3
        XCOPY "%2" "%1" /D /I %3

        this is NOT wath he wants, this would put the same files on the master as on the client, only the client should get the same files of the master. the master should remain unaltered



        a simple xcopy should do the trick, but it will only copy new files on the master to the client, and not delete files on the client that are no longer on the master

        I had a similar problem on my external hdd but i managed to get around the problem


        this batch file is run from my hdd (client)
        small part of my solution:
        Code: [Select]
        title = making music backup...
        xcopy /d /c /y "C:\Users\Polle\Music" "%~d0\Music"         this copys all music files from my pc (master) to my hdd (client)
        for /r "%~d0\Music\" %%F in (*.*) do echo %%F>> file.txt           this lists all music files on my hdd (client) and puts them in a file called file.txt
        for /F "tokens=2* delims=\" %%I in (file.txt) do if not exist "C:\Users\Polle\%%I\%%J" del "%~d0\%%I\%%J"      takes each entry in file.txt and checks if it exists on my pc (master), if not, it deletes the file from the hdd
        del file.txt    delete the list of files
        for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"   deletes all empty folders



        this should do the trick, but it will need some adjusting if you want to use it

        any of the gurus can improve this piece of code? I don't know that much of batch, but I managed to get around this problem this way, is there an easier or better way?

        donald99



          Rookie
        • Hello
        • Thanked: 2
          • Computer: Specs
          • Experience: Experienced
          • OS: Windows 7
          batch file to synchronize the contents of two folders
          « Reply #7 on: December 18, 2010, 05:46:22 PM »
          Good job.
          Try