WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

File Version and Dir Mgmt

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

How to Make Two Directories Identical

Keywords:      directory compare 

(Directory Synchronisation Module)
Here's a handy utility from making two directories identical. Typical use may be for updating a workstation's copy of a virus checker directory master held on a server. Optional overwrite of newer files, etc.

To Download a zip file with all of E.R.'s examples

;****************************************************************************
; Directory Synchronization Module (DSM)
; Written by E.Tippelt  100115.3301@compuserve.com
; Will synchronize the Source and Target directories
; Old files will be overwritten in the target
; Files not present in the source will be deleted in the target if required
;****************************************************************************
:DSM1
;Entry parameters:
;SourceDir="C:\windows"
;TargetDir="C:\test"
;Filetype="*.*"
;DeleteTargetFiles=1
;DowngradeTargetFiles=0
:DSM5                           ;Start Here
OrigDir=DirGet()                ;Save current Directory
if !DirExist("%SourceDir%")
  Message("Synchronize Error", "Source Directory does not exist")
  goto DSMEND
endif
:DSM10
if Filetype=="" then Filetype="*.*"
SourceList=Fileitemize("%SourceDir%\%Filetype%")
TargetList=Fileitemize("%TargetDir%\%Filetype%")
SourceNo=ItemCount(Sourcelist," ")      ;Count the entries in SourceList
if !DirExist("%TargetDir%") 
  DirMake("%TargetDir%")
  goto DSM50
endif
TargetNo=ItemCount(TargetList," ")      ;Count the entries in TargetList
if TargetNo==0 then goto DSM50
DirChange(TargetDir)
ErrorMode(@OFF)
FileAttrSet("*.*","rAsh")    ;Clear RSH/ Set A  target attributes for copy
                           ;in case there are existing files with set attribs
ErrorMode(@CANCEL)
for i=1 to SourceNo
  Item=ItemExtract(i,SourceList," ")        ;Get each entry in the source list
  Pos=ItemLocate(Item,TargetList," ")       ;See if present in target list
  if pos !=0
    TargetList=ItemRemove(pos,TargetList," ") ;if present, delete the item
    TargetNo=TargetNo-1
    If TargetNo==0 then break
  endif
next i
:DSM15  ;At this point, Targetlist contains a list of all the files not
        ;present on the source
if DeleteTargetFiles != 1 then goto DSM20    ;Skip delete process if delete flag not set
;OK, Lets delete some files
if FileDelete(TargetList)==@True then goto DSM20  ;Done it, so carry on
Message("Target Directory Delete Error","Error deleting files in %TargetDir%")  
:DSM20  ;This bit copies the Source files to the Target if not the same
DirChange(SourceDir)
for j=1 to SourceNo
  Item=ItemExtract(j,SourceList," ");Get each entry in the source list
  FC=FileCompare(Item,"%TargetDir%\%Item%")
  Switch FC
    Case 0                              ;Files are the same, so skip to next
      break
    Case 1                              ;Source file is same size, but newer, so copy it
      FileCopy("%Item%","%TargetDir%\%Item%",@false)
      break
    Case 2                              ;Source file is newer, so copy it
      FileCopy("%Item%","%TargetDir%\%Item%",@false)
      break
    Case 3                              ;Target file missing, so copy it
      FileCopy("%Item%","%TargetDir%\%Item%",@false)
      break
    Case FC                             ;Target must be newer, so go on to next file
      if DowngradeTargetFiles==0 then break ;unless downgrade requested
      FileCopy("%Item%","%TargetDir%\%Item%",@false) ;Downgrade requested, so copy anyway
  EndSwitch
:DSM22
next j                                       
goto DSMEND

:DSM50  ;This bit copies the Source files to a newly created (empty) Target 
DirChange(SourceDir)
if FileCopy(SourceList,"%TargetDir%",@false)==@true then goto DSM55
Message("File Copy Error","Error Copying Source to Target")
:DSM55
goto DSMEND
 
:DSMEND
DirChange(OrigDir)              ;Restore current Directory
RETURN

;****************************************************************************
; End of Directory Synchronization Module (DSM)
;****************************************************************************
 


Article ID:   W13783
Filename:   How to Make Two Directories Identical.txt
File Created: 2017:08:29:11:58:58
Last Updated: 2017:08:29:11:58:58