home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rexxlb.zip / SAMPLES / SYNCH.CMD < prev    next >
OS/2 REXX Batch file  |  1993-01-08  |  5KB  |  166 lines

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /* SYNCH: get two directory trees in synch. SYNCH compares two directories,  */
  4. /* an "old" version and a "new" version. All files that are newer in the new */
  5. /* directory are copied to the old directory, as are files that don't exist  */
  6. /* in the old directory. All files in the old directory that do not exist    */
  7. /* in the new directory are DELETED. If a file in the old directory is newer */
  8. /* than the file of the same name in the new directory, a synchronization    */
  9. /* error is detected, and no changes are made. The result is that unless     */
  10. /* there is an error, the two directories have the same contents. SYNCH      */
  11. /* loops recursively through all subdirectories of the old directory.        */
  12. /*                                                                           */
  13. /* Requires Personal REXX or REXXLIB (dosdir, dosdirpos, dosisdir, dosdel,   */
  14. /* doscd, dosfname, parsefn functions).                                      */
  15. /*                                                                           */
  16. /* Command format: SYNCH <old directory> <new directory>                     */
  17. /*                                                                           */
  18. /*****************************************************************************/
  19.  
  20. parse arg old new .
  21. if new = '' then do
  22.     say 'Syntax: SYNCH <old directory> <new directory>'
  23.     say '  Old directory = directory to be modified'
  24.     say '  New directory = new version of the directory'
  25.     exit 1
  26.     end
  27. call synch old, new
  28. exit
  29.  
  30. /* recursive subroutine to synch up directories */
  31. synch: procedure
  32. parse arg olddir, newdir
  33.  
  34. say ''
  35. olddir = fname(olddir)
  36. if olddir = '' then
  37.     return
  38. newdir = fname(newdir)
  39. if newdir = '' then
  40.     return
  41. call synchsub olddir, newdir
  42. name = dosdir(make_name(olddir, '*.*'), 'n', 'd', 'd')
  43. do while name \= ''
  44.     pos = dosdirpos()
  45.     if name \= '.' & name \= '..' then
  46.         call synch make_name(olddir, name), make_name(newdir, name)
  47.     name = dosdir(, 'n', 'd', 'd', pos)
  48.     end
  49. return
  50.  
  51. /* subroutine to synch up one pair of directories */
  52. synchsub: procedure
  53. parse arg old, new
  54. if \dosisdir(old) then do
  55.     say 'Directory' old 'not found.'
  56.     return
  57.     end
  58. if \dosisdir(new) then do
  59.     say 'Directory' new 'not found.'
  60.     return
  61.     end
  62. say 'Synchronizing' old 'with' new
  63. oldcount = 0
  64. newcount = 0
  65. olddate. = ''
  66. newdate. = ''
  67. /* gather information on old directory */
  68. name = dosdir(make_name(old, '*.*'), 'dtn')
  69. do while name \= ''
  70.     parse var name date time fn
  71.     oldcount = oldcount + 1
  72.     oldname.oldcount = fn
  73.     olddate.fn = date time
  74.     name = dosdir(, 'dtn')
  75.     end
  76. /* gather information on new directory */
  77. name = dosdir(make_name(new, '*.*'), 'dtn')
  78. do while name \= ''
  79.     parse var name date time fn
  80.     newcount = newcount + 1
  81.     newname.newcount = fn
  82.     newdate.fn = date time
  83.     name = dosdir(, 'dtn')
  84.     end
  85.  
  86. /* loop through old names */
  87. do i = 1 to oldcount
  88.     fn = oldname.i
  89.     date1 = olddate.fn
  90.     date2 = newdate.fn
  91.     oldname = make_name(old, fn)
  92.     newname = make_name(new, fn)
  93.     if date2 = '' then
  94.         call delete oldname
  95.     else if date2 = date1 then
  96.         iterate
  97.     else do
  98.         comp = compdate(date1, date2)
  99.         if comp < 0 then
  100.             call copy newname, oldname
  101.         else if comp > 0 then
  102.             say 'Synch error:' oldname 'is newer than' newname
  103.         end
  104.     end
  105.  
  106. /* loop through new names */
  107. do i = 1 to newcount
  108.     fn = newname.i
  109.     if olddate.fn = '' then
  110.         call copy make_name(new, fn), make_name(old, fn)
  111.     end
  112. return
  113.  
  114. /* delete a file */
  115. delete:
  116. say 'deleting' arg(1)
  117. call dosdel arg(1)
  118. return
  119.  
  120. /* copy a file */
  121. copy:
  122. say 'copy' arg(1) 'to' arg(2)
  123. '@copy' arg(1) arg(2) '>nul'
  124. return
  125.  
  126. /* test validity of directory names */
  127. fname: procedure
  128. dirname = arg(1)
  129. if length(dirname) = 2 & right(dirname,1) = ':' then
  130.     dirname = dirname||doscd(left(dirname,1))
  131. parse value parsefn(dirname) with . . fn ft
  132. if fn \= '-' then do
  133.     fullname = dosfname(dirname)
  134.     if fullname = '' then do
  135.         say 'Invalid name:' dirname
  136.         return ''
  137.         end
  138.     end
  139. else
  140.     fullname = dirname
  141. if \dosisdir(fullname) then do
  142.     say 'Not a directory:' fullname
  143.     return ''
  144.     end
  145. return fullname
  146.  
  147. /* compare two date-time paris */
  148. compdate: procedure
  149. parse arg mm1 '/' dd1 '/' yy1 time1, mm2 '/' dd2 '/' yy2 time2
  150. date1 = yy1||mm1||dd1||time1
  151. date2 = yy2||mm2||dd2||time2
  152. if date1 < date2 then
  153.     return -1
  154. else if date2 < date1 then
  155.     return 1
  156. else
  157.     return 0
  158.  
  159. /* make a name out of path and file id */
  160. make_name: procedure
  161. parse arg path, name
  162. if right(path, 1) = '\' | right(path, 1) = ':' then
  163.     return path||name
  164. else
  165.     return path'\'name
  166.