Computer Hope

Microsoft => Microsoft DOS => Topic started by: peer on December 17, 2010, 02:32:38 AM

Title: batch file to synchronize the contents of two folders
Post by: peer 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
Title: Re: batch file to synchronize the contents of two folders
Post by: polle123 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
Title: Re: batch file to synchronize the contents of two folders
Post by: reddevilggg on December 17, 2010, 09:40:08 AM

that should be easy,

Tell us how then!!
Title: Re: batch file to synchronize the contents of two folders
Post by: Geek-9pm 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.
Title: Re: batch file to synchronize the contents of two folders
Post by: Sidewinder 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 (http://www.microsoft.com/windowsxp/downloads/powertoys/xppowertoys.mspx) which is *censored* near idiot proof.  ;D

An alternative might be the RoboCopy GUI (http://www.softpedia.com/get/System/OS-Enhancements/RoboCopy-GUI.shtml). 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)
Title: Batch file to synchronize the contents of two folders
Post by: donald99 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
Title: Re: batch file to synchronize the contents of two folders
Post by: polle123 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?
Title: batch file to synchronize the contents of two folders
Post by: donald99 on December 18, 2010, 05:46:22 PM
Good job.