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

Directory and Deltree UDFs

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

Syncronize Directories

 Keywords: Syncronize Sync Directories Folders 


    #DefineFunction FileItemPathRecursive(filespec)
      fp = FilePath(filespec)
      fr = FileBaseName(filespec)
      fl = FileItemPath(filespec)
      dl = DirItemize(fp:"\*")
      dlc = ItemCount(dl, @TAB)
      For i = 1 To dlc
        t = FileItemPathRecursive(fp:ItemExtract(i, dl, @TAB):"\":fr)
        If t > "" Then fl = ItemInsert(t, -1, fl, @TAB)
      Next i
      Return fl
    #EndFunction


    #DefineFunction DirSyncRecursive(dir1, dir2)
    ; Makes dir2 same as dir1 by copying or deleting as needed
      If StrSub(dir1,StrLen(dir1),1)=="\" Then dir1 = StrSub(dir1,1,StrLen(dir1)-1)
      If StrSub(dir2,StrLen(dir2),1)=="\" Then dir2 = StrSub(dir2,1,StrLen(dir2)-1)
      If !DirExist(dir1) Then Return
      If !DirExist(dir2) Then DirMake(dir2)
      ; two sorted lists of full pathnames
      list1 = ItemSort(FileItemPathRecursive(dir1:"\*.*"),@TAB)
      list2 = ItemSort(FileItemPathRecursive(dir2:"\*.*"),@TAB)
      ; length of the unvarying part of the pathnames
      dln1 = StrLen(dir1)
      dln2 = StrLen(dir2)
      ; append stoppers to the lists
      stop = Num2Char(255) ; stop marker char
      list1 = ItemInsert(StrFill(stop,dln1+1),-1,list1,@TAB)
      list2 = ItemInsert(StrFill(stop,dln2+1),-1,list2,@TAB)
      maxlen = Max(ItemCount(list1,@TAB),ItemCount(list2,@TAB))
      i1 = 1 ; index to list 1
      i2 = 1 ; index to list 2
      While 1 ; parallel loop through both lists at once
        v1 = StrSub(ItemExtract(i1,list1,@TAB),dln1+1,-1) ; varying part from 1st list
        v2 = StrSub(ItemExtract(i2,list2,@TAB),dln2+1,-1) ; varying part from 2nd list
        If v1 == stop && v2 == stop Then Break ; hit both stoppers? were done.
        Switch StriCmp(v1,v2)
          Case -1 ; v1 is less, so there is no v2, so copy
              ErrorMode(@OFF) ; tolerate file i/o errors
                If !DirExist(FilePath(dir2:v1)) Then DirMake(FilePath(dir2:v1))
                FileCopy(dir1:v1, dir2:v1, @TRUE)
              ErrorMode(@ON)
              i1 = i1 + 1 ; move to see next from list 1
              Break
          Case  0 ; v1 == v2, so compare the actual file timecodes
              If FileTimeCode(dir1:v1) > FileTimeCode(dir2:v2)
                ErrorMode(@OFF) ; tolerate file i/o errors
                  FileCopy(dir1:v1, dir2:v1, @FALSE)
                ErrorMode(@ON)
              Else
                  ; do nothing
              EndIf
              i1 = i1 + 1 ; move to see next from list 1
              i2 = i2 + 1 ; move to see next from list 2
              Break
          Case  1 ; v2 is less, so v1 isn't there, so delete
              ErrorMode(@OFF) ; tolerate file i/o errors
              FileDelete(dir2:v2)
              ErrorMode(@ON)
              i2 = i2 + 1 ; move to see next from list 2
              Break
        EndSwitch
      EndWhile
      Return
    #EndFunction

Article ID:   W18358
Filename:   Syncronize Directories.txt
File Created: 2009:03:12:15:18:36
Last Updated: 2009:03:12:15:18:36