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

skip duplicate lines in an array

Status
Not open for further replies.

dsnyrs

Programmer
May 26, 2011
4
US
Hi,

I'm VERY new to programming and I've come across an issue that I need assistance on. I have a script that reads an OU in AD and places the group members into an array. I have the list sorted alphabetically. Now I need to have duplicate entries removed from the list. Something like this:

Group Name

A12 Company XYZ Super Users
A12 Company XYZUsers
T04 Client Name Super Users
T04 Client Name Users
etc.
etc.

What I'm trying to do ultimately, is get a list/count of Clients in the array to write to a file. Only 1 of the 2 Clients so the end result is:

A12 Company XYZUsers
T04 Client Name Users
etc.

What can I add to this code? Any help would be GREATLY appreciated!!!

Function Read()

Dim Count
Dim arrNames()
Dim objDuplicate

Dim objGroup, intSize, strUser, StrName, strHolder, objUser, i, j
intSize = 0

Set objGroup = GetObject("LDAP://CN=BLAH,OU=BLADGroups,OU=USERS,OU=BLAH,DC=BLAH,DC=BLAH,DC=COM")

For Each strUser in objGroup.Member
Set objUser = GetObject("LDAP://" & strUser)
ReDim Preserve arrNames(intSize)
arrNames(intSize) = objUser.CN
intSize = intSize + 1
Next

For i = (UBound(arrNames) - 1) to 0 Step -1
For j= 0 to i
If UCase(arrNames(j)) > UCase(arrNames(j+1)) Then
strHolder = arrNames(j+1)
arrNames(j+1) = arrNames(j)
arrNames(j) = strHolder
End If
Next
Next

For Each strName in arrNames
If Mid(strName,4,1) = " " Then
oFile.WriteLine(strName)
LineCount = UBound(arrNames)+ 1
End If
End If

Next

oFile.Close

End Function


 
What dictates a duplicate row? The beginning (eg. A12)?

As you iterate through the users, compare the "duplicate identifier" with the previous. No need for the other for..loops

Code:
[green]strPrevCN = ""[/green]
For Each strUser in objGroup.Member
    Set objUser =  GetObject("LDAP://" & strUser)
    [green]if (strPrevCN <> "") then
        if (left(strPrevCN, 3) <> left(objUser.CN, 3)) then
            ReDim Preserve arrNames(intSize)
            arrNames(intSize) = objUser.CN
            strPrevCN = objUser.CN
            intSize = intSize + 1
        end if
    end if[/green]
Next

-Geates

"I hope I can feel and see the change - stop the bleed inside a 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
 
I mean

Code:
strPrevCN = ""
For Each strUser in objGroup.Member
    Set objUser =  GetObject("LDAP://" & strUser)
    if (strPrevCN <> "") then
        [green]if (strPrevCN <> objUser.CN) then[/green]
            ReDim Preserve arrNames(intSize)
            arrNames(intSize) = objUser.CN
            strPrevCN = objUser.CN
            intSize = intSize + 1
        end if
    end if
Next

-Geates

"I hope I can feel and see the change - stop the bleed inside a 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
 
I've posted the entire script. This currently works but outputs ALL clients to the .txt file. I only need the filtered data written. Sorry for the confusion, I'm a first time Forum Poster!!! You help is appreciated!!

option explicit

' Global Initialization Section

Dim strPod : strPod = InputBox("What POD # would you like to report? (example 01)", "Provide POD #")
Dim objFSO : Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Dim objShl : Set objShl = CreateObject("WScript.Shell")
Dim strFilePath : strFilePath = "C:\filepath\"
Dim objFile : objFile = "POD" & strPod & "ClientGroups.txt"
Dim oFile : Set oFile = objFSO.CreateTextFile(strFilePath & objFile, True)
Dim LineCount

'Main Processing Section

Read()

View()

WScript.Quit


Function Read()

Dim Count
Dim arrNames()
Dim objDuplicate

Dim objGroup, intSize, strUser, StrName, strHolder, objUser, i, j
intSize = 0

Set objGroup = GetObject("LDAP://CN=BLAH,OU=BLADGroups,OU=USERS,OU=BLAH,DC=BLAH,DC=BLAH,DC=COM")

For Each strUser in objGroup.Member
Set objUser = GetObject("LDAP://" & strUser)
ReDim Preserve arrNames(intSize)
arrNames(intSize) = objUser.CN
intSize = intSize + 1
Next

For i = (UBound(arrNames) - 1) to 0 Step -1
For j= 0 to i
If UCase(arrNames(j)) > UCase(arrNames(j+1)) Then
strHolder = arrNames(j+1)
arrNames(j+1) = arrNames(j)
arrNames(j) = strHolder
End If
Next
Next

For Each strName in arrNames
If Mid(strName,4,1) = " " Then
oFile.WriteLine(strName)
LineCount = UBound(arrNames)+ 1
End If

Next

oFile.Close

End Function


Function View()

Dim objViewFile
Dim strFilePath : strFilePath = "filepath\"
Dim objFile : objFile = "POD" & strPod & "ClientGroups.txt"
'Set objVFile = "filepath"
WScript.Echo "There are " & LineCount & " Clients on Pod" & strPod

objViewFile = MsgBox("Would you like to view the file now?", 36, "View Results")

if objViewFile = 6 then

objShl.Run("notepad.exe " & strFilePath & objFile)

else
msgbox "Ok then, Have a nice day!!", 48, "Good-Bye"
end if

End Function

 
Code:
function Read()
   dim objGroup, objUser
   dim strPrevCN

   set objGroup = GetObject("LDAP://CN=BLAH,OU=BLADGroups,OU=USERS,OU=BLAH,DC=BLAH,DC=BLAH,DC=COM")

    strPrevCN = ""
    for each strUser in objGroup.Member
        set objUser =  GetObject("LDAP://" & strUser)
        if (strPrevCN <> "") then
            if (strPrevCN <> objUser.CN) then
                oFile.WiteLine objUser.CN
            end if
        end if
    next

    oFile.Close
end function

-Geates

"I hope I can feel and see the change - stop the bleed inside a 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