Solved

How Do I Synchronize Files Using Batch File?

Posted on 2004-04-22
12
2,648 Views
Last Modified: 2012-05-04
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.

leokuz
0
Comment
Question by:leokuz
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 8

Expert Comment

by:harris_c
ID: 10895971
There are a lot of free/shareware s/w that can do this.

http://download.com.com/3120-20-0.html?qt=folder+sync&tg=dl-2001&search=+Go%21+

hec",)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 10896077
You might also check this PAQ: http:Q_20458589.html
0
 
LVL 1

Accepted Solution

by:
jmwiens earned 300 total points
ID: 10896133
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.
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 100 total points
ID: 10896143
Aaaiii- that's what I was trying to remember.... LOL
Grab robocopy here: http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en
It really is the best option for what you're looking at doing.
0
 
LVL 3

Assisted Solution

by:elantra
elantra earned 100 total points
ID: 10896797
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 http://cheqsoft.com
'
'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
'
'INSTRUCTIONS
'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...
' http://msdn.microsoft.com/scripting/default.htm?/scripting/vbscript/
'===================

'===================

'SETTING 1 - WHERE TO BACKUP TO...
' 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

'END OF SETTING 1
'===================

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

'===================
'SETTING 2 - FILES AND/OR FOLDERS TO BACKUP...
'
'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) = ""

'END OF SETTING 2
'===================

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

'===================

'SETTING 3 - FOLDERS TO EXCLUDE... (optional)
'
'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) = ""

'END OF SETTING 3
'===================

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
wscript.quit

'----------------
'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(f1.name, 3) <> "tmp" then  'dont backup .tmp files

     backupfile = backfolder & f1.name

     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
  DoEvents

  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 & f.name

  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
   OutStream.WriteLine(oneline)
   if instr(Oneline, "----") then 'finds start of each backup log
    logcount = logcount + 1
     if logcount >= 5 then
      exit do
     end if
   end if
  Loop

  textstream.close
  OutStream.Close
  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 "
      wscript.quit
    end if  'left(BackupPath , len(MyData(p))) = MyData(p)
   end if ' not MyData(p) = ""
  next 'p

End Sub

'-------------------
'DISCLAIMER
'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.
'-------------------
0
 
LVL 1

Author Comment

by:leokuz
ID: 10897779
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 http://msdn.microsoft.com/scripting/default.htm?/scripting/vbscript/ 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).
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Expert Comment

by:jmwiens
ID: 10899894
Hi,
   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.
0
 
LVL 1

Author Comment

by:leokuz
ID: 10908918
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.
0
 
LVL 1

Expert Comment

by:jmwiens
ID: 10919320
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 *********************************************************
:Backup
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

:End


/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
0
 
LVL 1

Author Comment

by:leokuz
ID: 10920627
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.

leokuz

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?
0
 
LVL 1

Expert Comment

by:jmwiens
ID: 10920791
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...
0
 
LVL 1

Author Comment

by:leokuz
ID: 10920925
Thank you.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now