home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 18 REXX
/
18-REXX.zip
/
rexxlb.zip
/
SAMPLES
/
SYNCH.CMD
< prev
next >
Wrap
OS/2 REXX Batch file
|
1993-01-08
|
5KB
|
166 lines
/*****************************************************************************/
/* */
/* SYNCH: get two directory trees in synch. SYNCH compares two directories, */
/* an "old" version and a "new" version. All files that are newer in the new */
/* directory are copied to the old directory, as are files that don't exist */
/* in the old directory. All files in the old directory that do not exist */
/* in the new directory are DELETED. If a file in the old directory is newer */
/* than the file of the same name in the new directory, a synchronization */
/* error is detected, and no changes are made. The result is that unless */
/* there is an error, the two directories have the same contents. SYNCH */
/* loops recursively through all subdirectories of the old directory. */
/* */
/* Requires Personal REXX or REXXLIB (dosdir, dosdirpos, dosisdir, dosdel, */
/* doscd, dosfname, parsefn functions). */
/* */
/* Command format: SYNCH <old directory> <new directory> */
/* */
/*****************************************************************************/
parse arg old new .
if new = '' then do
say 'Syntax: SYNCH <old directory> <new directory>'
say ' Old directory = directory to be modified'
say ' New directory = new version of the directory'
exit 1
end
call synch old, new
exit
/* recursive subroutine to synch up directories */
synch: procedure
parse arg olddir, newdir
say ''
olddir = fname(olddir)
if olddir = '' then
return
newdir = fname(newdir)
if newdir = '' then
return
call synchsub olddir, newdir
name = dosdir(make_name(olddir, '*.*'), 'n', 'd', 'd')
do while name \= ''
pos = dosdirpos()
if name \= '.' & name \= '..' then
call synch make_name(olddir, name), make_name(newdir, name)
name = dosdir(, 'n', 'd', 'd', pos)
end
return
/* subroutine to synch up one pair of directories */
synchsub: procedure
parse arg old, new
if \dosisdir(old) then do
say 'Directory' old 'not found.'
return
end
if \dosisdir(new) then do
say 'Directory' new 'not found.'
return
end
say 'Synchronizing' old 'with' new
oldcount = 0
newcount = 0
olddate. = ''
newdate. = ''
/* gather information on old directory */
name = dosdir(make_name(old, '*.*'), 'dtn')
do while name \= ''
parse var name date time fn
oldcount = oldcount + 1
oldname.oldcount = fn
olddate.fn = date time
name = dosdir(, 'dtn')
end
/* gather information on new directory */
name = dosdir(make_name(new, '*.*'), 'dtn')
do while name \= ''
parse var name date time fn
newcount = newcount + 1
newname.newcount = fn
newdate.fn = date time
name = dosdir(, 'dtn')
end
/* loop through old names */
do i = 1 to oldcount
fn = oldname.i
date1 = olddate.fn
date2 = newdate.fn
oldname = make_name(old, fn)
newname = make_name(new, fn)
if date2 = '' then
call delete oldname
else if date2 = date1 then
iterate
else do
comp = compdate(date1, date2)
if comp < 0 then
call copy newname, oldname
else if comp > 0 then
say 'Synch error:' oldname 'is newer than' newname
end
end
/* loop through new names */
do i = 1 to newcount
fn = newname.i
if olddate.fn = '' then
call copy make_name(new, fn), make_name(old, fn)
end
return
/* delete a file */
delete:
say 'deleting' arg(1)
call dosdel arg(1)
return
/* copy a file */
copy:
say 'copy' arg(1) 'to' arg(2)
'@copy' arg(1) arg(2) '>nul'
return
/* test validity of directory names */
fname: procedure
dirname = arg(1)
if length(dirname) = 2 & right(dirname,1) = ':' then
dirname = dirname||doscd(left(dirname,1))
parse value parsefn(dirname) with . . fn ft
if fn \= '-' then do
fullname = dosfname(dirname)
if fullname = '' then do
say 'Invalid name:' dirname
return ''
end
end
else
fullname = dirname
if \dosisdir(fullname) then do
say 'Not a directory:' fullname
return ''
end
return fullname
/* compare two date-time paris */
compdate: procedure
parse arg mm1 '/' dd1 '/' yy1 time1, mm2 '/' dd2 '/' yy2 time2
date1 = yy1||mm1||dd1||time1
date2 = yy2||mm2||dd2||time2
if date1 < date2 then
return -1
else if date2 < date1 then
return 1
else
return 0
/* make a name out of path and file id */
make_name: procedure
parse arg path, name
if right(path, 1) = '\' | right(path, 1) = ':' then
return path||name
else
return path'\'name