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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search for discrepencies in file contents 2

Status
Not open for further replies.

MISMCSA

MIS
Jun 24, 2004
43
0
0
US
This is the scenario.

I have two text files with lists of user IDs. One file is a list of random user IDs and the other is a masterlist containing all user IDs that are part of a group.

What I want to do is write a script that cycles through every ID that is in list of random users and searches for that ID within the master list.

the object of this script is so I know which users, in the random list, are not part of the master list.

Thanks for your help!
 
You may consider a Dictionary object populated with the masterlist.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Would a collection work or do you think htat the dictionary object would be a better choice?
 
Dictionaries are faster than collections. It would also give the ability to expand beyond a simple data structure. I.E. you could have another dictionary stored as the value for a key that is the users ID. It would then be simple to access the user info.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
A Collection would work (in VBA) but VBScript doesn't have it. Furthermore, even in VBA, a Scripting.Dictionary object is much faster than a VBA.Collection.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I'm looking at example code for a dictionary object and the examples suggest typing in individual items.

Dim cars
Set cars = CreateObject("Scripting.Dictionary")
cars.Add "a", "Alvis"
cars.Add "b", "Buick"
cars.Add "c", "Cadillac"

How can I load my list of 4000 users into it?
 
Read the text file. If each userid is on its own line then something like:

strID = oFile.ReadLine()
oDic.Add strID, True


In this example I made the value be a boolean True, but it can be any variant. Including arrays or other dictionaries.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
How can if the ProDictionary has all of the user IDs in it?

'Define Variables
Dim fso, ReadFile, ProDictionary, StdDictionary
Dim strID

Set ProDictionary = CreateObject("Scripting.Dictionary")
'Set StdDictionary = CreateObject("Scripting.Dictionary")
Set fso = CreateObject("Scripting.FileSystemObject")

Set ReadFile = fso_OpenTextFile("c:\Documents and Settings\LT197\My Documents\Projects\OfficeXP Project\ProGroupIDs.txt", 1, false)

Do While ReadFile.AtEndOfStream <> True
strID = ReadFile.ReadLine()
ProDictionary.Add strID, True
Loop
ReadFile.close
 
I don't understand the question. It looks like your code will create a dictionary that has all of the userids from the text file for keys and True for all of the values. What is the problem?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Now, to test the random user, you simply use something this:
If ProDictionary.Exists(strJustReadID) Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Yea, sorry. I did not ask that question well at all. I must have been thinking to hard while I was typing.

I'm going to try a couple things and see what happens. thanks for your help.
 
I'm getting a type mismatch error with Ubound in the code below. Any ideas?

For i = 0 to Ubound(arrUserIDList)
If Not ProDictionary.Exists(arrUserIDList(i)) Then
WriteFile.WriteLine arrUserIDList(i)
End If
Next
 
Posting the whole code would certainly help ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
'Define Variables
Dim fso, ReadFile, ProDictionary, StdDictionary, arrUserIDList
Dim strID, strUserID

Set ProDictionary = CreateObject("Scripting.Dictionary")
'Set StdDictionary = CreateObject("Scripting.Dictionary")
Set fso = CreateObject("Scripting.FileSystemObject")

Set ReadFile = fso_OpenTextFile("c:\Documents and Settings\LT197\My Documents\Projects\OfficeXP Project\ProGroupIDs.txt", 1, false)

Do While ReadFile.AtEndOfStream <> True
strID = ReadFile.ReadLine()
ProDictionary.Add strID, True
Loop
ReadFile.close

Set ReadFile = fso_OpenTextFile("c:\Documents and Settings\LT197\My Documents\Projects\OfficeXP Project\UserIDs.txt", 1, false)
Set WriteFile = fso.CreateTextFile("c:\Documents and Settings\LT197\My Documents\Projects\OfficeXP Project\NotMemberofGroup.txt", True)

Do Until ReadFile.AtEndOfStream
strUserID = ReadFile.ReadLine()
arrUserIDList = strUserID
Loop

For i = 0 to Ubound(arrUserIDList)
If Not ProDictionary.Exists(arrUserIDList(i)) Then
WriteFile.WriteLine arrUserIDList(i)
End If
Next
 
To test your loop (which looked fine to me) I did this which worked:
Code:
Option Explicit
Dim i, arrUserIDList, ProDictionary

Set ProDictionary = CreateObject("Scripting.Dictionary")
arrUserIDList = Array("ID1", "ID2", "ID3")


For i = 0 to Ubound(arrUserIDList)
        If Not ProDictionary.Exists(arrUserIDList(i)) Then
            WScript.Echo arrUserIDList(i)
        End If
Next
Is this the only place that you iterate an array?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
arrUserIDList = strUserID
arrUserIDList is not an array, so the UBound function will choke.
You may consider replacing this:
Do Until ReadFile.AtEndOfStream
strUserID = ReadFile.ReadLine()
arrUserIDList = strUserID
Loop
By this:
arrUserIDList = Split(ReadFile.ReadAll, vbCrLf)


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Good eye PHV. I was looking at the wrong piece.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
As I was reading through my code I was thinking it wasn't an array. I was just replacing the value every time, wasn't I?

Anyway, what you gave me apparently worked, but I need to verify the results.

You guys have been extreemely helpful!
 
It works to a certain extent. However, a new issue has arisen. I am comparing strings in this script and it just so happens that there are spaces after a few of these IDs and it recognizes the spaces as being a different ID. Is there an adjustment, that can be made within the code, that will eliminate spaces?
 
Replace this:
ProDictionary.Add strID, True
By this:
ProDictionary.Add Trim(strID), True
And this:
If Not ProDictionary.Exists(arrUserIDList(i)) Then
By this:
If Not ProDictionary.Exists(Trim(arrUserIDList(i))) Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top