Page 1 of 1 1
Topic Options
#207186 - 2013-04-24 11:28 AM Compare txt files
jjpski Offline
Just in Town

Registered: 2010-02-13
Posts: 4
Loc: Japan
Hi,
What is the best way to compare two files and write the unmatched values to a new txt.

File A = Patches required
File B = Patches installed

I need to compare the values in file A to file B. The values that do not exist in file B compared to file A are written to a new txt. Additional layer to this problem values in B may look like this "Package_for_KB2823180~31bf3856ad364e35~x86~~6.1.1.0" and the value in file A would be "KB2823180". I searched the forums and just got more confused.

Thanks Jay

Top
#207189 - 2013-04-24 02:47 PM Re: Compare txt files [Re: jjpski]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
There are several methods but here's something simple..

Load each file into an array - look at the FileIO UDF -
 Code:
$Array = FileIO('filename', 'R')
Call this once for each file, using different names for the $Array variable ($aNeed and $aHave, for example) and source filename.

Enumerate the "have" array, locate the matching record in the Need array and clear that value.
 Code:
For Each $Patch in $aHave
  $E = AScan($aNeed, $Patch) ; look for the current patch in the NEED array
  If $E >= 0           ; will be -1 if not found
    $aNeed[$E] = ''    ; clear the value from the NEED array
  EndIf
Next
The $aNeed array will contain only the missing patches.

Optionally, use the QS or QSort UDF to sort the array. Save the resulting array.
 Code:
$aNeed = QSort($aNeed) ; sort
$ = FileIO(@WSKTA + '_Need.txt', 'W', $aNeed, 2)
The option "2" on FileIO will strip the blank lines. This will create a computer-specific file containing the patches that are needed.

Glenn

PS - all of the UDFs I referenced can be found in the Kix UDF Library on my web site - link in sig. Most are here on KORG, but may be outdated.
_________________________
Actually I am a Rocket Scientist! \:D

Top
#207215 - 2013-04-27 10:04 AM Re: Compare txt files [Re: Glenn Barnas]
jjpski Offline
Just in Town

Registered: 2010-02-13
Posts: 4
Loc: Japan
Glenn,

This worked perfectly. Had to modify my registry enumeration txt file for the values to used with FileIO. Awesome! Thanks!


Jay

Top
#207217 - 2013-04-27 03:38 PM Re: Compare txt files [Re: jjpski]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4403
Loc: New Jersey
Jay,

Glad it worked! (and thanks for the follow-up!)

Check the AScan manual page - you'll find that there's an option to find a substring match, which might let you reverse the search logic and not have to edit your data.

Since the HAVE array has more text than the NEED array, you could enumerate the NEED array instead, using the substring flag of AScan. You'd then clear the enumerated element instead of the found element - AScan would simply indicate a match without caring where it matched. To do this, you'd need the form For $I = 0 to UBound($aNeed) so you have a reference to the index of the enumerated array. A For Each $ in $Array won't let you modify the enumerated array.

Just some thoughts on an alternative method...

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#207229 - 2013-05-01 07:05 PM Re: Compare txt files [Re: Glenn Barnas]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
If you have control of the file and are able to write to an INI file that would probably be the easiest way to manage it.
Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Henriques) and 2516 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.054 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org