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.

File Synchronisation Module

Keywords:   file synchronization 

Here's a handy utility for "synchronising" a named group of files between a source directory and a target directory. Think of it as an intelligent copy command, which can optionally overwrite newer files in the target directory, or leave them alone. (File Synchronisation Module)

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


;****************************************************************************
; File Synchronization Module (FSM)
; Written by E.Tippelt  100115.3301@compuserve.com
; Will copy files from the Source to the Target directory
; Old files will be overwritten in the target
; Newer files in the target will not be overwritten unless requested
; Files not present in the source will be deleted in the target if required
;****************************************************************************
:FSM1
;Entry parameters:
;SourceDir=
;TargetDir=
;Sourcelist= No Wildcards!!!
;DowngradeTargetFiles=0
:FSM5                           ;Start Here
OrigDir=DirGet()                ;Save current Directory
if !DirExist("%SourceDir%")
  Message("Synchronize Error", "Source Directory does not exist")
  goto FSMEND
endif
:FSM10
SourceNo=ItemCount(Sourcelist," ")      ;Count the entries in SourceList
if !DirExist("%TargetDir%") 
  DirMake("%TargetDir%")
  goto FSM50
endif
TargetList=Fileitemize("%TargetDir%\*.*")
TargetNo=ItemCount(TargetList," ")      ;Count the entries in TargetList
if TargetNo==0 then goto FSM50          ;Empty directory, so copy all
DirChange(TargetDir)
ErrorMode(@OFF)
FileAttrSet(Sourcelist,"rAsh")  ;Clear RSH/ Set A  target attributes for copy
                                ;in case there are existing files with set attribs
ErrorMode(@CANCEL)
:FSM20  ;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
:FSM22
next j                                       
goto FSMEND

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

;****************************************************************************
; End of File Synchronization Module (FSM)
;****************************************************************************


Article ID:   W13802
Filename:   Synchronize Files between Source and Target Example 2.txt
File Created: 2017:08:29:11:19:24
Last Updated: 2017:08:29:11:19:24