home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rexxlb.zip / SAMPLES / UPDIR.CMD < prev   
OS/2 REXX Batch file  |  1993-02-05  |  4KB  |  145 lines

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /* UPDIR: update a target directory from a given source directory. All       */
  4. /* files in the target directory are compared to files of the same name      */
  5. /* in the source directory. If they are older, they are replaced by the      */
  6. /* newer file from the source directory. No other changes are made.          */
  7. /*                                                                           */
  8. /* Requires Personal REXX or REXXLIB (dosdrive, doscd, dosdir, dosdirpos,    */
  9. /* dosisdir, parsefn, upper functions).                                      */
  10. /*                                                                           */
  11. /* Command format: UPDIR <target> <source>                                   */
  12. /*                                                                           */
  13. /*****************************************************************************/
  14.  
  15. parse arg dir1 dir2 .
  16. if dir1 = '' then do
  17.     say 'Disk or directory not specified.'
  18.     exit 1
  19.     end
  20. if dir2 = '' then
  21.     dir2 = dosdrive()':'doscd()
  22. if right(dir2, 1) = ':' then
  23.     dir2 = dir2||doscd(dir2)
  24. if \dosisdir(dir2) then do
  25.     say dir2 'not found.'
  26.     exit 2
  27.     end
  28.  
  29. parse value parsedname(dir1) with drive ',' path ',' fileid
  30. parse value parsedname(dir2) with drive2 ',' path2 ',' fileid2
  31.  
  32. dir1 = drive':'path
  33. dir2 = drive2':'path2
  34. fullid = dir1||fileid
  35. parse upper value fullid dir1 dir2 with fullid dir1 dir2
  36. file = dosdir(fullid)
  37. if file = '' then do
  38.     say 'Files "'fullid'" not found.'
  39.     exit 2
  40.     end
  41. newi = 0
  42. neweri = 0
  43. olderi = 0
  44. samei = 0
  45.  
  46. say 'Updating' dir1 'from' dir2 '...'
  47. do while file \= ''
  48.     position = dosdirpos()
  49.     parse var file fname . fdate ftime .
  50.     newfile = dosdir(dir2||fname)
  51.     parse var newfile newfname . newfdate newftime .
  52.     comp = compdate(fdate ftime,newfdate newftime)
  53.     select
  54.         when newfile = '' then do
  55.             newi = newi + 1
  56.             new.newi = fname
  57.             end
  58.         when comp < 0 then do
  59.             olderi = olderi + 1
  60.             older.olderi = fname
  61.             end
  62.         when comp > 0 then do
  63.             neweri = neweri + 1
  64.             newer.neweri = fname
  65.             end
  66.         otherwise do
  67.             samei = samei + 1
  68.             same.samei = fname
  69.             end
  70.         end
  71.     file = dosdir(,,,,position)
  72.     end
  73.  
  74. sp = '     '
  75. if olderi > 0 then do
  76.     say 'The following files are older on' dir1 'than on' dir2
  77.     say 'The older version will be updated.'
  78.     call replace older, olderi
  79.     end
  80. if neweri > 0 then do
  81.     say 'The following files are newer on' dir1 'than on' dir2
  82.     call show newer, neweri
  83.     end
  84. if newi > 0 then do
  85.     say 'The following files are on' dir1 'but not on' dir2
  86.     call show new, newi
  87.     end
  88. exit
  89.  
  90. /* compare two date-time paris */
  91. compdate: procedure
  92. parse arg mm1 '/' dd1 '/' yy1 time1, mm2 '/' dd2 '/' yy2 time2
  93. date1 = yy1||mm1||dd1||time1
  94. date2 = yy2||mm2||dd2||time2
  95. if date1 < date2 then
  96.     return -1
  97. else if date2 < date1
  98.     then return 1
  99. else
  100.     return 0
  101.  
  102. /* parse a file identifier */
  103. parsedname: procedure
  104. x = upper(arg(1))
  105. parsed = parsefn(x)
  106. if parsed = '' then
  107.     parsed = '- - - -'
  108. parse var parsed drive path fn ft
  109. if drive = '-' then
  110.     drive = dosdrive()
  111. if path = '-' then
  112.     path = doscd(drive)
  113. if right(path,1) \= '\' then
  114.     path = path'\'
  115. if fn = '-' then
  116.     fileid = '*.*'
  117. else if ft = '-' then
  118.     fileid = fn
  119. else
  120.     fileid = fn'.'ft
  121. return drive','path','fileid
  122.  
  123. /* display file names */
  124. show:
  125. line = ' '
  126. do x = 1 to arg(2)
  127.     line = line left(value(arg(1)'.x'), 14)
  128.     if x // 5 = 0 then do
  129.         say line
  130.         line = ' '
  131.         end
  132.     end
  133. if line \= '' then
  134.     say line
  135. return
  136.  
  137. /* update a group of files */
  138. replace:
  139. do x = 1 to arg(2)
  140.     fname = value(arg(1).x)
  141.     say fname
  142.     'copy' dir2||fname dir1||fname
  143.     end
  144. return
  145.