Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


compare 2 files

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

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> compare 2 files
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 compare 2 files - 6/22/2008 10:55:49 PM   
  tomas

 

Posts: 11
Score: 0
Joined: 1/2/2008
Status: offline
Hi, I'm trying to figure out how to compare 2 files...
I have "file 1.txt" and "file 2.txt", I need all lines in "file 2.txt" checked against each line of "file 1.txt". Here's what I do...

File 1.txt;
file 1 line 1
file 1 line 2
file 1 line 3
file 1 line 4
file 1 line 5
file 1 line 6

File 2.txt
file 2 line 1
file 2 line 2
file 2 line 3

script;
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile1 = objFSO.OpenTextFile _
   ("file1.txt", ForReading)
Set objTextFile2 = objFSO.OpenTextFile _
   ("file2.txt", ForReading)

Do Until objTextFile1.AtEndOfStream
   strNextLine1 = objTextFile1.Readline
   wscript.echo strNextLine1
Do Until objTextFile2.AtEndOfStream
   strNextLine2 = objTextFile2.Readline
   wscript.echo strNextLine2
Loop
Loop


I'm getting following output;
file 1 line 1
file 2 line 1
file 2 line 2
file 2 line 3
file 1 line 2
file 1 line 3
file 1 line 4
file 1 line 5
file 1 line 6

I was expecting;
file 1 line 1
file 2 line 1
file 2 line 2
file 2 line 3
file 1 line 2
file 2 line 1
file 2 line 2
file 2 line 3
file 1 line 3
file 2 line 1
file 2 line 2
file 2 line 3
file 1 line 4
file 2 line 1
file 2 line 2
file 2 line 3
file 1 line 5
file 2 line 1
file 2 line 2
file 2 line 3
file 1 line 6
file 2 line 1
file 2 line 2
file 2 line 3

 
Can you please tell me what did I do wrong in the script?


 
 
Post #: 1
 
 RE: compare 2 files - 6/22/2008 11:04:24 PM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi tomas,

you open the second file just once, you must open (and close!) it
for each line of the first one.

More important: what do you want to achieve? Can't you use fc
to compare the files?

Regards

ehvbs

(in reply to tomas)
 
 
Post #: 2
 
 RE: compare 2 files - 6/22/2008 11:53:01 PM   
  tomas

 

Posts: 11
Score: 0
Joined: 1/2/2008
Status: offline
quote:

ORIGINAL: ehvbs

Hi tomas,

you open the second file just once, you must open (and close!) it
for each line of the first one.

More important: what do you want to achieve? Can't you use fc
to compare the files?

Regards

ehvbs


ehvbs,
Thanks for the suggestion.

What I'm doing is getting members from LDAP group, saving all ID's to file1,
then get all ID's from Users OU and save to file2.
I'm at the stage where I go through all user ID's from file2 (Users OU) and compare each user ID from file1 (members of a group) to find user ID's that are not part of the group.
In theory instead of saving both user lists to files I could write those to array.Then compare for each member of array1 to array2

(in reply to ehvbs)
 
 
Post #: 3
 
 RE: compare 2 files - 6/23/2008 12:09:54 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi tomas,

(1) instead of arrays/nested loops you could use dictionaries (fast lookup) or even
    SQL joins

(2) could you post a small/faked example (both of the files, expected result)

Regards

ehvbs

(in reply to tomas)
 
 
Post #: 4
 
 RE: compare 2 files - 6/23/2008 12:44:59 AM   
  tomas

 

Posts: 11
Score: 0
Joined: 1/2/2008
Status: offline
quote:

ORIGINAL: ehvbs

Hi tomas,

(1) instead of arrays/nested loops you could use dictionaries (fast lookup) or even
   SQL joins

(2) could you post a small/faked example (both of the files, expected result)

Regards

ehvbs


1 . Could you please clarify further on that?

2. Files;

script that gets user ID's from Users OU;
Set oContainer = GetObject("LDAP:// OU=users,DC=super,DC=com"")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set OutPutFileADID = objFSO.CreateTextFile("network IDs.txt", True)

For Each oUser In oContainer
    OutPutFileADID.WriteLine oUser.Get("name")
Next

Wscript.Quit
 
script that gets members of the group;
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set OutPutFileADID = objFSO.CreateTextFile("members of.txt", True)


Set objGroup = GetObject ("LDAP:// CN=Group,OU=Groups,DC=super,DC=com")
objGroup.getInfo

arrMemberOf = objGroup.GetEx("member")
   For Each strMember in arrMemberOf
     strMember = Mid(strMember, 4, 330)
     arrGroup = Split(strMember, "," )
     eid = arrGroup(0)
     OutPutFileADID.WriteLine eid     
  Next

Wscript.Quit

All user ID's from Users are written to network IDs.txt, user ID's that are part of group CN=Group are written to members of.txt 
I'm trying to get user ID's that are not part of the group CN=Group.

(in reply to ehvbs)
 
 
Post #: 5
 
 RE: compare 2 files - 6/23/2008 2:03:45 AM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi tomas,

given two files with user names:

nwids.txt
Anna
Bill
Carmen
Dave

grpms.txt
Bill
Dave

in a directory containg a schema.ini file containing:
[nwids.txt]
Format=TabDelimited
ColNameHeader=False
Col1=sName Char Width 50

[grpms.txt]
Format=TabDelimited
ColNameHeader=False
Col1=sName Char Width 50

this script:


      

output:


      

While the dictionary approach may look easier at first, the SQL method scales much better.

Regards

ehvbs

(in reply to tomas)
 
 
Post #: 6
 
 RE: compare 2 files - 6/25/2008 7:55:58 PM   
  tomas

 

Posts: 11
Score: 0
Joined: 1/2/2008
Status: offline
Thanks very much
I still don't understand how the script works, but it does!!!

Cheers!

(in reply to ehvbs)
 
 
Post #: 7
 
 RE: compare 2 files - 6/25/2008 11:03:51 PM   
  ehvbs

 

Posts: 2201
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi tomas,

you are welcome. Can you specify, what you don't understand? I'm willing
to try to answer your questions.

Regards

ehvbs

(in reply to tomas)
 
 
Post #: 8
 
 
 
  

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 >> compare 2 files Page: [1]
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