How Do I Synchronize Files Using Batch File?

I would like to automatically synchronize folder with subfolders (with different kind of files in them) located lets say on drive D:\ (in windows 2003 server) with another folder on same D drive using bat file (or some other file). In other words I want an identical copy (except of main folder as two foders with same name may not co-exist in same directory) of original folders with subfolders and files be created on the same drive and updated every three hours if additional files moved into source folders or more folders created, but I don't want source folders and files be updated vice versa, thus from its copy. Hope my question is not too confusing. I'd like it to operate in quite mode with no errors, confirmation or any kind of pop up messages on the screen. However, overwriting in this case is OK and actually GOOD, but with not confirmation or other "pop ups."

Thank you.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

There are a lot of free/shareware s/w that can do this.

You might also check this PAQ: http:Q_20458589.html
If I understand what you're asking, correctly, you should really try robocopy.  It's an excellent tool from Microsoft that's included in most resource kits.  It can easily create a mirror image of a subdirectory tree and it's great at copying security and even copying locked files.

I don't have robocopy at home but you'd use it similar to this:
  robocopy.exe \\source\dir \\dest\dir -r -purge
(This would copy everything from the source directory to the destination directory and make sure that it is an exact match)

It won't copy files that don't need to be copied and it will delete files that no longer exist in the source directory.

If this sounds like what you're looking for, I can provide more details.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Aaaiii- that's what I was trying to remember.... LOL
Grab robocopy here:
It really is the best option for what you're looking at doing.
Here is a script you can use...  put it in <NAME>.vbs (just name the file with a .VBS extension).  This script does incremental backups and instructions on how to use the script are below.  I removed most the msgboxes that you would normally encounter.  Follow the directions in the Instructions field below...  After you place the script in a location outside the directory you want to copy (backup), you can use task scheduler to schedule this script to run every 3 hours.  This script will only backup/copy those files that have changed so it will be much more efficient and faster than other methods.  Enjoy!


'Tom's Cool Backup Script 16 JAN 2001
'Written by Tom Hingston of
'This script copies all the files that are new or have changed,
'to the backup folder specified by BackupPath.
'It also logs the files copied from the last 5 backups to C:\Backuplog.txt
'You need to change settings in 2 places below... Setting 1 is where to
'backup to; Setting 2 is what to backup. There is optional Setting 3 which
'allows you to exclude some sub-folders from within the folders being backed up.
'All file/folder paths need to be inside speech marks "Like this"
'or otherwise the speech marks need to be empty ""
' Learn about VBScript at...


' Set BackupPath
' BackupPath is the Folder that you want to backup to....
' Example1: BackupPath = "H:\Backup"
' Example2: BackupPath = "\\Tom\C\Quality Documents"

BackupPath = "H:\Backup" ' <-- Set backup path here


If Wscript.Arguments.Count = 0 Then 'not initiated by dropping folder on it

'These are the Files and/or Folders that you want to backup.

Quantity = 10 '<-- This can be increased if MyData(?) increases.
redim MyData(Quantity)

' These are the Folders that you want to backup...
' EXAMPLE: MyData(1) = "C:\Data"

MyData(1) = "C:\Toms data" ' <-- Set these
MyData(2) = ""
MyData(3) = ""
MyData(4) = ""
MyData(5) = ""
MyData(6) = ""
MyData(7) = ""
MyData(8) = ""
MyData(9) = ""
MyData(10) = ""


  redim MyData(1)
  MyData(1) = Wscript.Arguments(0) 'was initiated by dropping folder on it
  Quantity = 1
End If


'These are Sub-Folders within the folders being backed up,
'that you can Exclude from the backup.
'EXAMPLE: Excl_Data(1) = "C:\Data\Kids files"

Excl_Quantity = 5 '<-- This can be increased if Excl_Data(?) increases.
redim Excl_Data(Excl_Quantity)

Excl_Data(1) = "" '<-- Set these if required
Excl_Data(2) = ""
Excl_Data(3) = ""
Excl_Data(4) = ""
Excl_Data(5) = ""


set fso = CreateObject("Scripting.FileSystemObject")
count = 0
MyDelay = 1 '(mS) that cause script delays to allow system to still beused (doevents)
dim arrResults ' array to store results in
redim arrResults(0)

If right(BackupPath, 1) <> "\" then
  BackupPath =   BackupPath & "\"  'inserts the slash as it is required later
End If

call CheckPath

if not fso.folderExists ( BackupPath ) then
  fso.CreateFolder( BackupPath )
end if

'sets drv for MakeFolderPath
If left(BackupPath , 1) = "\" then 'network
  for ss = 1 to len(BackupPath )
    strCh = mid(BackupPath , ss, 1)
    if strCh = "\" then countslash = countslash + 1
    if countslash = 3 then
      drv = ss + 3
    end if
  next 'ss
  if not countslash >= 3 then msgbox "Error in script relative to network path"
else 'local drive letter
  drv = 5
End If

For i = 1 to Quantity
  If MyData(i) <> "" then

    if fso.DriveExists( MyData(i) ) Then      'it is a drive
      call backup( MyData(i) )

    elseif fso.folderExists(MyData(i)) then   'it is a folder
      call MakeFolderPath( BackupPath & right(MyData(i), len(MyData(i))-3 ) )
      call backup( MyData(i) )

    elseif fso.fileExists(MyData(i)) then     'it is a file
      call FileBackup( MyData(i) )

    else                                      'not a drive or folder or file
      msgbox MyData(i) & vbcrlf & vbcrlf & "This file/folder does not appear to exist" & _
                 vbcrlf & "Please ensure you have typed it correctly or that" & vbcrlf & _
                 "you have not moved, renamed or deleted it.", 48, "File or Folder Error"
    end if

  End If
Next 'i

strlog = "-------" & vbnewline & "Backup on " & now() & vbnewline
strlog = strlog & join(arrResults, vbnewline) & vbnewline & "= " & count & " files copied to " & BackupPath & vbnewline & vbnewline
call logresults ( strlog )

Set WshShell = nothing
set fso = nothing

'Performes the actual copying if required
Sub Backup( mypath )

 if ExcludeF( mypath ) = False then
  Set f = fso.GetFolder( myPath )
  Set fc = f.Files
  For Each f1 in fc
    wscript.sleep MyDelay  '= DoEvents
    If not fso.folderExists( BackupPath  & right(myPath, len(myPath)-3 )) then
       call MakeFolderPath( BackupPath & right(myPath, len(myPath)-3 ))
    End if
   If fso.DriveExists( mypath ) Then 'it is a drive
      backfolder = BackupPath
   else 'it is a folder
      backfolder =  BackupPath & right(f, len(f)-3 ) & "\"
   end if

   if right(, 3) <> "tmp" then  'dont backup .tmp files

     backupfile = backfolder &

     If fso.fileExists( backupfile ) then
       Set bf = fso.GetFile( backupfile )
       if f1.DateLastModified <> bf.DateLastModified then
         fso.CopyFile f1, backfolder, true
         count = count + 1
         ReDim Preserve arrResults(count)
         arrResults(count) = f1
       end if
     Else  'does not yet exist in backup
       fso.CopyFile f1, backfolder, true
       count = count + 1
       ReDim Preserve arrResults(count)
       arrResults(count) = f1
     End if
   end if ' that checked for filetype .tmp

  Next  'f1

  For Each Folder In f.SubFolders
    Call Backup(Folder)
  Next 'Folder

 End if 'Excludefolder( mypath ) = False
End Sub

'performs the actual copying of Files if the path was a file - not directory
Sub FileBackup( myFile )

  set f = fso.GetFile( myFile )
  backupfile = BackupPath &

  If fso.fileExists( backupfile ) then
       Set bf = fso.GetFile( backupfile )
       if f.DateLastModified <> bf.DateLastModified then
         fso.CopyFile f, BackupPath , true
         count = count + 1
         ReDim Preserve arrResults(count)
         arrResults(count) = f
       end if
  Else         'does not yet exist in backup
       fso.CopyFile f, BackupPath , true
       count = count + 1
       ReDim Preserve arrResults(count)
       arrResults(count) = f
  End if    'fso.fileExists( backupfile ) then

End Sub

'if the folder does not yet exist in the backup path, make it.
Sub MakeFolderPath( myfolder )
 For x = drv to len( myFolder )
  MyChr = mid( myFolder , x, 1)
  if mychr = "\" then
    xfolder = left(myfolder, x-1)
    if not fso.folderExists ( xFolder ) then
      fso.CreateFolder( xFolder )
    end if
  end if
 Next  'x
 if not fso.folderExists ( myFolder ) then
  fso.CreateFolder( myFolder )
 end if
End sub

'function that writes results to the log.txt
Sub LogResults( myText )
  myfile = "C:\Backuplog.txt"

  Set fso = CreateObject("Scripting.FileSystemObject")
  OutFile = "C:\#temp#.txt"
  set textstream = fso.OpenTextFile(myFile,1,true)
  Set OutStream=fso.CreateTextFile(OutFile,True)

  OutStream.WriteLine( mytext )

  Do until textstream.AtEndOfStream  'writes existing text to temp file
   OneLine = textstream.ReadLine
   if instr(Oneline, "----") then 'finds start of each backup log
    logcount = logcount + 1
     if logcount >= 5 then
      exit do
     end if
   end if

  fso.CopyFile OutFile, myfile, true
  fso.DeleteFile OutFile
End Sub


Function ExcludeF(qF)
'On error resume next
  for q = 1 to Excl_Quantity
    if right(Excl_Data(q), 1) = "\" then
      Excl_Data(q) = left(Excl_Data(q), len(Excl_Data(q))-1) 'removes last \
    end if

    If qF = Excl_Data(q) then
      ExcludeF = True
      Exit Function
    End if
  Next 'q

  ExcludeF = False
End Function


Function Excludefolder(qFolder)
'On error resume next
  for q = 1 to Excl_Quantity
    if right(Excl_Data(q), 1) = "\" then
      Excl_Data(q) = left(Excl_Data(q), len(Excl_Data(q))-1) 'removes last \
    end if

    If qFolder = Excl_Data(q) then
      Excludefolder = True
      Exit Function
    End if
  Next 'q

  Excludefolder = False
End Function


Sub DoEvents
  'To cause script delays to allow system to still be used (doevents)
  On error resume next
  wscript.sleep 1  'milliseconds
End Sub

Sub CheckPath
  'To ensure the backup path is not inside a folder being backed up

  for p = 1 to Quantity
   if not MyData(p) = "" then
    if lcase(left(BackupPath , len(MyData(p)))) = lcase(MyData(p)) then
      msgbox "You cannot back up a folder to a folder inside it" & vbcrlf &_
      "because it will also backup the backup etc." & vbcrlf & vbcrlf &_
      "Please use a different BackupPath." & vbcrlf & vbcrlf &_
      "This backup has been cancelled.", 64, "Error in BackupPath "
    end if  'left(BackupPath , len(MyData(p))) = MyData(p)
   end if ' not MyData(p) = ""
  next 'p

End Sub

'The Software Product is provided "as is"; I make no warranty,
'whether express or implied, of the merchantability of
'the Software Product or its fitness for any particular purpose.

'In no circumstances will I be liable for any damage,
'loss of profits, loss of data, goodwill or for any indirect or
'consequential loss arising out of your use of the Software Product,
'or inability to use the Software Product, even if I
'have been advised of the possibility of such loss.

'Use of our software implies your acceptance of this disclaimer.
leokuzAuthor Commented:
In regards to elantra's code it works great by copying directories, but for some reason it did not copy all folders, probably because some folders had no files in them or because I left SETTING 3 unchanged (as is). Also, when I deleted some files and folders in the source directories, it did not update the "backup" directories, but did update if I added files or folders with files to source. Also, couple of times when file ended running some sort of JavaScript (or something else) popup message display for short time notifying of task result and then disappearing. This is first time I had to deal with .vbs, so I went to to get "knowledge", but got personal apologies from Microsoft for the inconvenience of not finding subject at said address.

I would like to get more details from jmwiens on Robocop as he mentioned that "it will delete files that no longer exist in the source directory." Could you please jmwiens? Thanks. I must though give some credit to sirbounty for providing quick and easy "access" to Robocop (I just downloaded it from the page he provided, but did not install yet).
   I'm on vacation until Monday and I don't have access to my stuff but I'll provide you the script I use first thing Monday morning.  I use it as a way to backup all my user's files when they logon.  It works very well, is very robust, and most importantly, is VERY quick.  There are a TON of robocopy command line options so check them out by typing: robocopy.exe /?
   Also, make sure you have a more recent version of robocopy.  There are a lot of versions out there but the more recent ones do shadow copies of locked files.  I believe it says "XP version" when you execute the help command.
leokuzAuthor Commented:
Question to elantra! Could you please check my qestion "Move Files Automatically" dated 04/21/2004. Could your code do that if modified? If only renaming same name files would prevent from successfull code, then how about just leaving same name files (not moving them)? Or . . . how about moving them but to specifically created subfolder for these purpose? Thank you.
As promised, here's the simple script I use to make backups when the user logs in.  See below for the explanation of the command line options I used.

@echo off

rem *******************************************
rem *** Backup script
rem *** by Jeff Wiens
rem *** 4-2-2004 - Created initial script
rem *******************************************

rem *********************************************************
rem *** Do we need to copy robocopy.exe to the local drive?
rem *********************************************************
if exist %SystemRoot%\System32\robocopy.exe goto Backup
copy \\server03\tools\robocopy.exe %SystemRoot%\System32\

rem *********************************************************
rem *** Copy Documents
rem *********************************************************
echo Performing a backup of "My Documents" to your network drive...
robocopy "%UserProfile%\My Documents" "%HomeShare%\AutoBackup-%ComputerName%\My Documents" *.* /s /b /copy:DAT /purge /r:2 /w:1 /np /tee /log:%SystemDrive%\backup.log /xd RECYCLER /XF *.mpg *.wma *.mp3 *.wav *.wmv *.avi *.mpeg *.bkf

echo Performing a backup of "Favorites" to your network drive...
robocopy "%UserProfile%\Favorites" "%HomeShare%\AutoBackup-%ComputerName%\Favorites" *.* /s /b /copy:DAT /purge /r:2 /w:1 /np /tee /log+:%SystemDrive%\backup.log

echo Performing a backup of Outlook Personal Folders and Outlook Express Data...
robocopy "%UserProfile%\Application Data" "%HomeShare%\AutoBackup-%ComputerName%\Application Data" *.pst *.dbx /s /b /copy:DAT /purge /r:2 /w:1 /np /tee /log+:%SystemDrive%\backup.log

if not exist %SystemDrive%\Informs goto End
echo Copying your Informs data...
robocopy "%SystemDrive%\Informs\Database" "%HomeShare%\AutoBackup-%ComputerName%\Informs" *.* /s /b /copy:DAT /purge /r:2 /w:1 /np /tee /log+:%SystemDrive%\backup.log


/s = Copy subdirectories
/b = Use backup mode if the file is locked
/copy:DAT = copy various file attributes
/purge = Delete files on destination if they don't exist on the source
/r:2 = Retry file twice if it's locked
/w:1 = Wait 1 second before retrying a locked file
/np = Don't show progress
/tee = Output to console AND log file
/XD = Exclude following directories
/XF = Exclude following file types
leokuzAuthor Commented:
Thank you. That is a great help. Though even just typing robocopy /? at command prompt, I could see wonderful features Robocopy offers.

Thanks again so much for taking my attention to this utility.


P.S. Oh! The other question I have about robocopy is whether it is possible to schedule it to run without command prompt window opening (kind of distirbing), and is there difference between scheduling it through "AT" utility at command prompt and through built-in windows scheduler (in control panel) using .bat files with combination of commands you mentioned above?
You can run robocopy without a command prompt window if you execute it via a script or other executable.  That gets a little complicated unless you're familar with the Windows Scripting Host.

As for the AT versus the built-in windows scheduler, I'm not sure.  I've used them both but I don't know if they're connected or not...
leokuzAuthor Commented:
Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.