Listing 1
/*------------------------------------------------------------------------*\
| |
| 9412LS01.CMD (rxls02) - Compare two directories using associative arrays |
| |
\*------------------------------------------------------------------------*/
call RxFuncAdd 'SysLoadFuncs', 'REXXUTIL', 'SysLoadFuncs' /* 0006 */
call SysLoadFuncs /* 0007 */
/* 0008 */
directory_1 = 'd:\*.*' /* 0009 */
directory_2 = 'e:\*.*' /* 0010 */
/* 0011 */
/*-----------------------*\ /* 0012 */
| Process 1st directory | /* 0013 */
\*-----------------------*/ /* 0014 */
index_1. = 0 /* initialize all elements */ /* 0015 */
call SysFileTree directory_1, 'tree_1', 'FO' /* 0016 */
do t1 = 1 to tree_1.0 /* 0017 */
subscript = FILESPEC( 'P', tree_1.t1 ) ||, /* 0018 */
FILESPEC( 'N', tree_1.t1 ) /* 0019 */
index_1.subscript = t1 /* 0020 */
end /* 0021 */
/* 0022 */
/*-------------------------------*\ /* 0023 */
| Process 2nd directory vs. 1st | /* 0024 */
\*-------------------------------*/ /* 0025 */
index_2. = 0 /* initialize all elements */ /* 0026 */
call SysFileTree directory_2, 'tree_2', 'FO' /* 0027 */
do t2 = 1 to tree_2.0 /* 0028 */
subscript = FILESPEC( 'P', tree_2.t2 ) ||, /* 0029 */
FILESPEC( 'N', tree_2.t2 ) /* 0030 */
index_2.subscript = t2 /* 0031 */
if index_1.subscript > 0 then /* 0032 */
do /* 0033 */
index_1.subscript = -1 /* indicate matching entry */ /* 0034 */
iterate /* 0035 */
end /* 0036 */
say directory_1 ||, /* 0037 */
' does not contain ' ||, /* 0038 */
subscript /* 0039 */
end /* 0040 */
/* 0041 */
/*--------------------------------------------*\ /* 0042 */
| Process unmatched entries in 1st directory | /* 0043 */
\*--------------------------------------------*/ /* 0044 */
do t1 = 1 to tree_1.0 /* 0045 */
subscript = FILESPEC( 'P', tree_1.t1 ) ||, /* 0046 */
FILESPEC( 'N', tree_1.t1 ) /* 0047 */
if index_1.subscript > 0 then /* 0048 */
do /* 0049 */
say directory_2 ||, /* 0050 */
' does not contain ' ||, /* 0051 */
subscript /* 0052 */
end /* 0053 */
end /* 0054 */
/* 0055 */
exit /* 0056 */
Lines 15 - 21 initialize the associative array for the first directory, index-1, to zero
and then build both the sequential array and the associative array for the directory.
Lines 26 - 40 does the same for the second directory as well as writing a message to
the screen for all files that do not occur in the first directory (the corresponding index entry
does not have a value greater than zero). Also, if the directory_2 entry is found to exist
in directory_1, the index for the directory_1 entry is set to minus one to distinguish
it from the positive value it had when the index was built.
Finally, lines 45 - 54 list any entries in the first directory that do not exist in the
second directory (the index value is neither 0 or -1 ).
As you can see in Listing 1, only three iterations are necessary to compare both directories
against each other regardless of the number of items in each directory. This provides
significant savings over any other technique when you are dealing with unsorted lists.