Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Find the differences in two text files

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,2642
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Find the differences in two text files
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Find the differences in two text files - 4/11/2005 5:30:19 AM   
  esnmb

 

Posts: 441
Score: 0
Joined: 1/11/2005
From: USA
Status: offline
I have two text files. Each has a list of names. I need to compare them to each other then create a third text file with ONLY the differences.

I have no code for this at this time as I don't really know where to start. I can open both files and get all the contents with ReadAll, but then what?
 
 
Post #: 1
 
 Re: Find the differences in two text files - 4/11/2005 5:44:43 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
load the contents of file one into a dictionary
read in the second file (i prefer readlines)
check to see if the name exists in the dictionary
if so, continue the loop
if not add to another dictionary
output dictionary when its done reading the second file

(in reply to esnmb)
 
 
Post #: 2
 
 Re: Find the differences in two text files - 4/11/2005 5:50:20 AM   
  esnmb

 

Posts: 441
Score: 0
Joined: 1/11/2005
From: USA
Status: offline
I'll take stab at this...

(in reply to esnmb)
 
 
Post #: 3
 
 Re: Find the differences in two text files - 4/11/2005 5:55:09 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
if not let me know, it should be pretty straight forward.

(in reply to esnmb)
 
 
Post #: 4
 
 Re: Find the differences in two text files - 4/11/2005 6:07:39 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
There are a couple of ways to do this. In addition to what kirrilian suggested, I have another. This one has the benefits of having the big O to N*M[+N*M] as opposed to N^M (M being the number of files).

======================================================================
Option Explicit

Dim fso, temp, hash, file1, file2, ts, hash2, key

file1 = "F:\file1.txt"
file2 = "F:\file2.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set hash = CreateObject("Scripting.Dictionary")
Set hash2 = CreateObject("Scripting.Dictionary")

Set ts = fso.OpenTextFile(file1,1)
Do Until ts.AtEndOfStream
temp = ts.ReadLine
hash.Item(temp) = 1
Loop
ts.Close

Set ts = fso.OpenTextFile(file2,1)
Do Until ts.AtEndOfStream
temp = ts.ReadLine
If hash.Exists(temp) Then
hash.Item(temp) = hash.Item(temp) + 1
Else
hash.Item(temp) = 1
End If
Loop

For Each key In hash.Keys
If hash(key) = 1 Then
hash2(key) = True
End If
Next

For Each key In hash2.Keys
WScript.Echo key
Next

(in reply to esnmb)
 
 
Post #: 5
 
 Re: Find the differences in two text files - 4/11/2005 6:10:38 AM   
  esnmb

 

Posts: 441
Score: 0
Joined: 1/11/2005
From: USA
Status: offline
I can't seem to figure out how to do this. I can add a key , but I need to add an Item as well which I'm not sure what to put there. Pretty confused on this one. I haven't really used dictionaries very often.

My attemp:

On Error Resume Next
Const TextMode = 1

Set objDictionary = CreateObject("Scripting.Dictionary")
objDictionary.CompareMode = TextMode

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("c:\All.csv")
Set objFile1 = objFSO.CreateTextFile("c:\Group.csv")
Set objFile2 = objFSO.CreateTextFile("c:\Diff.csv")

Set objOU = GetObject _
("LDAP://ou=Users,ou=All Users,dc=mlnusa,dc=com")

ObjOU.Filter = Array("user")

For Each objUser in objOU
objFile.WriteLine Chr(34) & objUser.CN & Chr(34) & ","
arrMemberOf = objUser.GetEx("memberOf")
For Each Group in arrMemberOf
If InStr(Group, "CN=Microsoft IP Printing") Then
objFile1.WriteLine Chr(34) & objUser.CN & Chr(34) & ","
End If
Next
Next

objFile.ReadLine
objFile1.ReadAll

'--- LOST

objFile.Close
objFile1.Close
objFile2.Close

MsgBox "The script has completed.",,"STATUS"

(in reply to esnmb)
 
 
Post #: 6
 
 Re: Find the differences in two text files - 4/11/2005 6:20:35 AM   
  esnmb

 

Posts: 441
Score: 0
Joined: 1/11/2005
From: USA
Status: offline
Wow I'm confused. I'm trying to understand what your script is saying token. I'll try to incorporate it into my script.

(in reply to esnmb)
 
 
Post #: 7
 
 Re: Find the differences in two text files - 4/11/2005 6:27:58 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
The idea is pretty simple. Suppose you have 2 lists of names. When you add them into one big list, names that occur twice will exist in both smaller list while a name that exist only once are unique (exists in only one of the list). This unique name is then the DIFFERENCE between the 2 list of names. =)

(in reply to esnmb)
 
 
Post #: 8
 
 Re: Find the differences in two text files - 4/11/2005 6:30:21 AM   
  esnmb

 

Posts: 441
Score: 0
Joined: 1/11/2005
From: USA
Status: offline
Gotcha. I'm working on your script now. I'll let you know how it goes. :)

(in reply to esnmb)
 
 
Post #: 9
 
 Re: Find the differences in two text files - 4/11/2005 6:33:25 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
Sure =)

(in reply to esnmb)
 
 
Post #: 10
 
 Re: Find the differences in two text files - 4/11/2005 6:34:34 AM   
  esnmb

 

Posts: 441
Score: 0
Joined: 1/11/2005
From: USA
Status: offline
So this is where the Dictionary is getting populated?

Do Until ts.AtEndOfStream
temp = ts.ReadLine
If hash.Exists(temp) Then
hash.Item(temp) = hash.Item(temp) + 1
Else
hash.Item(temp) = 1
End If
Loop

(in reply to esnmb)
 
 
Post #: 11
 
 Re: Find the differences in two text files - 4/11/2005 6:36:01 AM   
  esnmb

 

Posts: 441
Score: 0
Joined: 1/11/2005
From: USA
Status: offline
Ok, this is my script, but nothing is happening. My CPU is spiked, but besides that nothing is happening.

On Error Resume Next
Const TextMode = 1

Set hash = CreateObject("Scripting.Dictionary")
Set hash2 = CreateObject("Scripting.Dictionary")
objDictionary.CompareMode = TextMode

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("c:\All.txt")
Set objFile1 = objFSO.CreateTextFile("c:\Group.txt")
Set objFile2 = objFSO.CreateTextFile("c:\Diff.txt")

Set objOU = GetObject _
("LDAP://ou=Users,ou=All Users,dc=mlnusa,dc=com")

ObjOU.Filter = Array("user")

For Each objUser in objOU
objFile.WriteLine objUser.CN
arrMemberOf = objUser.GetEx("memberOf")
For Each Group in arrMemberOf
If InStr(Group, "CN=Microsoft IP Printing") Then
objFile1.WriteLine objUser.CN
End If
Next
Next

objFile.Close
objFile1.Close
objFile2.Close

file2 = "c:\All.txt"
file1 = "c:\Group.txt"

Set ts = fso.OpenTextFile(file1,1)
Do Until ts.AtEndOfStream
temp = ts.ReadLine
hash.Item(temp) = 1
Loop
ts.Close

Set ts = fso.OpenTextFile(file2,1)

Do Until ts.AtEndOfStream
temp = ts.ReadLine
If hash.Exists(temp) Then
hash.Item(temp) = hash.Item(temp) + 1
Else
hash.Item(temp) = 1
End If
Loop

For Each key In hash.Keys
If hash(key) = 1 Then
hash2(key) = True
End If
Next

For Each key In hash2.Keys
WScript.Echo key
Next

(in reply to esnmb)
 
 
Post #: 12
 
 Re: Find the differences in two text files - 4/11/2005 6:37:17 AM   
  kirrilian


Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status: offline
quote:
Originally posted by token

The idea is pretty simple. Suppose you have 2 lists of names. When you add them into one big list, names that occur twice will exist in both smaller list while a name that exist only once are unique (exists in only one of the list). This unique name is then the DIFFERENCE between the 2 list of names. =)





good point

(in reply to esnmb)
 
 
Post #: 13
 
 Re: Find the differences in two text files - 4/11/2005 6:37:24 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
quote:
Originally posted by esnmb

So this is where the Dictionary is getting populated?

Do Until ts.AtEndOfStream
temp = ts.ReadLine
If hash.Exists(temp) Then
hash.Item(temp) = hash.Item(temp) + 1
Else
hash.Item(temp) = 1
End If
Loop



Yes, it contains a list of ALL names from the two text files.

(in reply to esnmb)
 
 
Post #: 14
 
 Re: Find the differences in two text files - 4/11/2005 6:42:11 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
What are the contents of both "c:\All.txt" and "c:\Group.txt" ?

I don't suppose they are the same, are they ?

(in reply to esnmb)
 
 
Post #: 15
 
 Re: Find the differences in two text files - 4/11/2005 6:44:06 AM   
  esnmb

 

Posts: 441
Score: 0
Joined: 1/11/2005
From: USA
Status: offline
So you are adding all the names from one list into the Key and all the names of the other list into the Item?

(in reply to esnmb)
 
 
Post #: 16
 
 Re: Find the differences in two text files - 4/11/2005 6:45:31 AM   
  esnmb

 

Posts: 441
Score: 0
Joined: 1/11/2005
From: USA
Status: offline
All.txt are all user names in our domain. Group.txt are all user names in a specific Group.

Group.txt is the smaller list of course.

(in reply to esnmb)
 
 
Post #: 17
 
 Re: Find the differences in two text files - 4/11/2005 6:55:21 AM   
  esnmb

 

Posts: 441
Score: 0
Joined: 1/11/2005
From: USA
Status: offline
It worked. I had objFSO bound from my script, but didn't notice that you had just fso. I changed yours to objFSO and it worked. Still trying to really understand what you did.

Thanks everyone!

(in reply to esnmb)
 
 
Post #: 18
 
 Re: Find the differences in two text files - 4/11/2005 7:20:17 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
No, all names are added as Keys, and the number of occurances are the item

No problem.

(in reply to esnmb)
 
 
Post #: 19
 
 Re: Find the differences in two text files - 4/11/2005 7:21:53 AM   
  token

 

Posts: 1917
Score: 0
Joined: 1/14/2005
From:
Status: offline
If you didn't spot this, it should be changed too.

objDictionary.CompareMode = TextMode

to

hash.CompareMode = TextMode
hash1.CompareMode = TextMode

(in reply to esnmb)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Find the differences in two text files Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts