Differences Between Files
Systems administrators often need to compare and contrast a couple of files. For example, you might have one file that contains unique usernames and unique machine names and a second file that contains unique machine names and IP addresses, and you want to find the machines in file 1 that don't have an IP address in file 2 and the machines in file 2 that don't have a corresponding username in file 1. Or you might have a case like mine, in which two files with hundreds of thousands of keys and values in lines of data were supposed to contain the same values but were unsorted and had missing data and thus were more difficult to compare. Let's take a look at how you tackle these kinds of problems.
The second problem is easier to solve, so let's look at that one first. You can't just sort the files and compare them line by line because after you run into a missing line in one of the files, every subsequent line generates an error. You could work around this problem by writing a routine that, when it encounters a missing line, skips that line and checks the next one. You could also read the files into an array and have a couple of loops check every item in array 1 to see whether it's in array 2, but the Dictionary object lets you write much simpler code. . . .
<BR>
25: While Not filInput1.AtEndOfStream <BR>
<DD>26: strLine = filInput1.ReadLine <BR>
<DD>27: dicData1.Add Split(strLine,",")(0), Split(strLine,",")(3) <BR>
28: Wend <BR>
<BR>
I get the error: <I>_05.vbs(27, 2) Microsoft VBScript runtime error: This key is already associated with an element of this collection.</I> Any ideas on why I get this error? The line numbers were added by me after running code. I used a comma deliminated file with a .txt ending not .csv. </P>
Jim Hunter July 16, 2003