Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare 2 text files

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
Hello All ... To start this properly, I am quite new to vbscript so please excuse my ignorance. I am looking for a simple way to compare 2 text files which contain several lines of header information. After that comes part numbers which I am most interested in. I would like to then output common part numbers that are found in both text files. The part numbers all have a pound sign before them so I guess this would be the unique identifier for what to look at. The text files look much like this with header info:

Part Number 9-788-01; 7/2/2012; 9-788-01-TOP
Left Bracket Assembly

#CA299-000
#CA111-200
#AR344-01GH

There are usually 100 lines in each text file. So what I want to do is ignore anything in any text file except for what comes after a pound sign. Find matching part numbers in the 2 files and output them to a separate text file.

Any ideas how to do this? Thanks in advance for any help! Your time is greatly appreciated.
 
The # before the part number helps tremedously. Open the file, read through it (I like line-by-line) looking for words that start with #. Copy them to an array. Repeat. Merge arrays. Output arrays to file. Is there only 1 part number per line? And the line starts with #? If so... (untested)

Code:
set objFSO = CreateObject("Scripting.FileSystemObject")

function findPartNumbers(strFileName)
    set objFile = objFSO.OpenTextFile(strFileName, 2 true, 0)
    do until objFile.AtEndOfStream
        strLine = objFile.ReadLine
        if (left(strLine, 1) = "#") then
            strPartNumbers = strPartNumbers & trim(strLine) & vbNewLine
        end if
    loop
    set objFile = nothing
    findPartNumbers = split(strPartNumbers, vbNewLine)
end function

function mergeArrays(arrA, arrB)
    set objDictionary = CreateObject("Scripting.Dictionary")

    'add items in arrA if they don't exist
    for each str in arrA
        if NOT (objDictionary.exists(str) then objDictionary.add(str)
    next

    'add items in arrB if they don't exist
    for each str in arrB
        if NOT (objDictionary.exists(str) then objDictionary.add(str)
    next

    'add the items of the dictionary to a string to be split into an array
    for each key in objDictionary.keys
        strItems = strItems & key & vbNewLine
    next
    mergeArrays = split(strItems, vbNewLine)
end function

function outputArrayTo(strFileName, arr)
    set objFile = objFSO.OpenTextFile(strFileName, 1, true, 0)
    for each str in arr
        objFile.WriteLine str
    next
end function

'begin
arrListA = findPartNumbers("c:\partsA.dat")
arrListB = findPartNumbers("c:\partsB.dat")
arrPartNumbers = mergeArrays(arrListA, arrListB)
outputArrayTo("C:\merged_part_numbers.txt", arrPartNumbers)

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Hello Geates,

I am trying to get this to work and it seems there were a few missing parentheses and I was able to get through those. I got to the last line and got an error where the bracket was outside of the calling sub. I changed that and now Line 5 char 5 says "bad file mode" and that is where I am stuck now. Please help!

Thanks
 
Ah. In function findPartNumbers(), change the 2 to a 1.
Code:
    set objFile = objFSO.OpenTextFile(strFileName, [red]1[/red] true, 0)

and do the opposite in function outputArrayTo()
Code:
    set objFile = objFSO.OpenTextFile(strFileName, [red]2[/red] true, 0)

This number is the file mode. Reading = 1, Writing = 2. I had it backwards.

-Geates


"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Hello Geates... thanks again for your continued help on this. I fixed the lines you pointed out and now I get an error in this line of code and says there is a wrong number of arguments or invalid property assignment for 'objDictionary.add'

Code:
if NOT (objDictionary.exists(str)) then objDictionary.add(str)

Thanks for your help!
 
Oops. The dictionary .add() method calls for a key, item pair. In this case, the item is the same as the key.
Code:
if NOT (objDictionary.exists(str)) then objDictionary.add str[red], str[/red]

[link]http://raqxtr.sstar.com/caspdoc/html/vbscript_language_reference.htm[/url] is a great reference.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top