Guidance
指路人
g.yi.org
Guidance Forums / Reginald Rexx / Comparing files

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Comparing files
#12408
Posted by: Michael S 2008-10-11 17:29:19 Last edited by: Michael S 2008-10-11 17:32:35 (Total edited 1 time)
Has anyone written something in Rexx (yes, I know there are umpteen cheap packages out there) that will compare 2 files against each other for matches/mismatches.

The reason I ask is this. I have written a front-end reginald application whereby a user can select one or more SQL tables and dump selected columns and selected rows to a simple text file. The resulting file could be something like this
Column1(1)              Content1   <--- First column, first row of data
Column2(1)              Content2
Column3(1)              Content3
Column1(2)              Content4   <--- First column, second row of data
Column2(2)              Content5
etc etc
The idea is that they then run my application to produce this file, run their "real" application that updates/deletes/inserts rows into the tables and then run the same front-end above (which will obviously produce a file with slightly different contents). They can then compare these results using something like Beyond Compare or ExamDiff or similar.

Here comes the rub. We have an application that is already used by developers. This uses an application written by a consultant in Perl (which noone here "speaks"). The "trouble" with this application is partly being Perl, partly being the fact that any changes to the selection (or new tables) have to be coded/compiled in Perl (whereas my front-end is a bit point-and-shoot).

The end result from the Perl application is a couple of files (before and after) which are then compared using Perl's built-in (?) traverse-sequences, and where the results are shown colour-coded in Excel. With a bit of effort, I can write the Excel VBA code so that it can create a similar layout, but I need something that will create/show me the actual file differences.

There you go. Anyone out there bothered to write a home-grown file compare ?
Message2.
#12410
Posted by: PeterJ 2008-10-11 19:40:09
Yes, I did! It's an exact compare, therefore it's not the fastest proc especially for large files.

Peter

/* -------------------------------------------------------------------------
 * Compares 2 files.
 * result = compare(filename1, filename2,<option>)
 *
 * filename1 =	The name of the first file.
 * filename2 =	The name of the second file.
 * Returns   : 0 if files are the same 
 *           > 0  files are not equal, if DETAILS option is
 *                defined, the position where the first difference
 *                occurs is returned     
 * option    : undefined, just difference is identified. This means
 *             different lengths lead immediately to a not equal 
 *             situation, without an identification where it occurs.   
 * DETAILS     file is compared even if length differ, the exact 
 *             position of difference is returned
 * ---------------------------------------------------------------------- */
compare: PROCEDURE
PARSE ARG file1, file2, option
rc1=STREAM(file1, 'C', 'OPEN READ')
rc2=STREAM(file2, 'C', 'OPEN READ')
IF rc1 == 'READY:' & rc2 == 'READY:' THEN rc=filecompare(option)
   ELSE rc=1
STREAM(file1, 'C', 'CLOSE')
STREAM(file2, 'C', 'CLOSE')
RETURN rc
/* -----------------------------------------------------------------------
* ---------------------------------------------------------------------- */
filecompare:
PARSE ARG details
/* Get the sizes of both files */
l1 = CHARS(file1)
l2 = CHARS(file2)
/* Do the sizes match? If not, then the files can't be the same */
IF details \= 'DETAILS' THEN IF l1 \= l2 THEN RETURN 8
/* They're the same size, so now we have to compare the actual
 * contents.
 */
rlen = 64000
IF rlen > l1 THEN rlen = l1
ctr = 0
DO UNTIL l1 < 1
   ctr = ctr + 1
   f1 = CHARIN(file1,,rlen)
   f2 = CHARIN(file2,,rlen)
/*	Read error before EOF */
   IF f1 == '' THEN RETURN l1 
   IF f2 == '' THEN RETURN l1
   IF f1 \= f2 THEN RETURN l1 
   l1 = l1 - rlen
END
/* They're the same */
RETURN 0
Message3. Thanks Peter
#12411
Posted by: Michael S 2008-10-11 20:07:51
but I was more after a program that SHOWED the differences (yes, I know that's a lot more complicated), but I thought I'd throw out my question anyway.
Message4.
#12412
Posted by: PeterJ 2008-10-11 20:13:44
Wait a sec, I might also have something. I check my files
Message5.
#12413
Posted by: PeterJ 2008-10-12 16:18:45
The attached procs do a compare on byte level. The result is returned in a stem
differences.x, the array contains all the mismatches, mismatch count is in differences.0, differences.i contains the position of the mismatch.

Warning for large files with loads of mismatches it takes a while and consumes a lot of CPU. if there are only few mismatches it runs ok, even for larger files.
 

file1='C:\Temp\Temp1.txt'
file2='C:\Temp\Temp2.txt'
SAY  clcl(file1,file2) 'result'
DO i=1 TO differences.0
   SAY i differences.i
   IF i>100 THEN LEAVE
END
EXIT 
/* -------------------------------------------------------------------------
 * Compares 2 files.
 * result = compare(filename1, filename2,<option>)
 *
 * filename1 =	The name of the first file.
 * filename2 =	The name of the second file.
 * Returns   : 0 if files are the same 
 *           > 0 files are not equal, if DETAILS option is
 *               defined, the position where the first difference
 *               occurs is returned     
 * ---------------------------------------------------------------------- */
clcl: PROCEDURE EXPOSE differences.
PARSE ARG file1, file2
rc1=STREAM(file1, 'C', 'OPEN READ')
rc2=STREAM(file2, 'C', 'OPEN READ')
IF rc1 == 'READY:' & rc2 == 'READY:' THEN rc=fileclcl()
   ELSE rc=1
STREAM(file1, 'C', 'CLOSE')
STREAM(file2, 'C', 'CLOSE')
RETURN rc
/* -----------------------------------------------------------------------
 * String Compare 
 * ---------------------------------------------------------------------- 
 */
fileclcl:
/* Get the sizes of both files */
l1 = CHARS(file1)
l2 = CHARS(file2)
rlen = 16000
IF rlen > l1 THEN rlen = l1
mismatchctr = 0
offset=0
clcrc=0
DO UNTIL l1 < 1
   f1 = CHARIN(file1,,rlen)
   f2 = CHARIN(file2,,rlen)
   IF f1 \= f2 THEN clcrc=clcdetails(offset)
   l1 = l1 - rlen
   offset=offset+rlen
END
/* They're the same */
RETURN clcrc
/* -----------------------------------------------------------------------
 * String Compare 
 * ---------------------------------------------------------------------- 
 */
clcdetails:
PARSE ARG foffset
len1=LENGTH(f1)
len2=LENGTH(f2)
IF len1>len2 THEN count=len1
   ELSE count=len2
DO ci=1 TO count
   IF SUBSTR(f1,ci,1) \= SUBSTR(f2,ci,1) THEN DO
      mismatchctr = mismatchctr+1
      differences.mismatchctr=foffset+ci
      differences.0=mismatchctr
   END
END
RETURN 8
Forum List • Thread List • Reply • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Wed 2024-2-21  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0