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

Join array into string a reverse order

Status
Not open for further replies.

cluM09

Technical User
May 15, 2004
127
US
Hi,

I have a script that I parse the array, but I want to join the array into a string in the rverse order, but I have problems. Can anyone please help!

The code I have so far is as follows:

Dim strUserDN, strOU, strOUS
strUserDN = "CN=COMPUTERNAME,OU=MSCS,OU=Windows 2000,OU=Servers - OU,OU=Restricted,DC=MYDOMAIN,DC=COM"

Wscript.Echo Parse1(strUserDN)
Wscript.Echo Parse2(strUserDN)
Wscript.Echo Parse3(strUserDN)
strOUS = Parse2(strUserDN)

'Read into array
Dim arrOUList()
i = 0
For Each strOU in Split(strOUS, ",")
Redim Preserve arrOUList(i)
arrOUList(i) = Mid(strOU, InStr(strOU, "=") + 1)
i = i + 1
Next

'Read array in reverse order
k = 0
For k = Ubound(arrOUList) to LBound(arrOUList) Step - 1
Wscript.Echo arrOUList(k)
Next
MyArray = Join(arrOUList, "\")

Wscript.Echo MyArray

Function Parse1(strDN)
Parse1 = Mid(strDN, InStr(strDN, "=") + 1)
Parse1 = Mid(Parse1, InStr(Parse1, "=") + 1)
Parse1 = MId(Parse1, 1, InStr(Parse1, "=") - 4)
End Function

Function Parse2(strDN)
Parse2 = Mid(strDN, InStr(strDN, "=") + 1)
Parse2 = Mid(Parse2, InStr(Parse2, "=") - 2)
Parse2 = Left(Parse2, InStr(UCase(Parse2), "DC=") - 2)
End Function

Function Parse3(strDN)
Parse3 = Mid(strDN, InStr(strDN, "=") + 1)
Parse3 = Mid(Parse3, InStr(Parse3, "=") - 2)
End Function

Basically, I can read the array in the reverse order, but I do not know how to join the array items in the reverse order back into a string.

Thanks guys!
 
uh, that would start at zero and go down...

Dim NewString
NewString = ""
For k = Ubound(arrOUList) to LBound(arrOUList) Step - 1
NewString = NewString & arrOUList(k)
Next
 
yeap, knew something didnt feel right in the grey stuff when i clicked on the submit button :), its been a long day and its only 17:58:22
 
mrmovie,

Thanks for the response! The code you have will not parse the array the reverse order.

Basically, what I need is to get the final string MyArray to contain the following content:

Restricted\Servers - OU\Windows 2000\MSCS

instead of

MSCS\Windows 2000\Servers - OU\Restricted

CluM09
 
what are you actually trying to with this LDAP/AD type string??? if you dont mind me asking?
 
Sheco,

Thanks so much for the help! Yes, it works. The final code is then:

Dim strUserDN, strOU, strOUS
strUserDN = "CN=COMPUTERNAME,OU=MSCS,OU=Windows 2000,OU=Servers - OU,OU=Restricted,DC=MYDOMAIN,DC=COM"

Wscript.Echo Parse1(strUserDN)
Wscript.Echo Parse2(strUserDN)
Wscript.Echo Parse3(strUserDN)
strOUS = Parse2(strUserDN)

'Read into array
Dim arrOUList()
i = 0
For Each strOU in Split(strOUS, ",")
Redim Preserve arrOUList(i)
arrOUList(i) = Mid(strOU, InStr(strOU, "=") + 1)
i = i + 1
Next

'Read array in reverse order back into a string
Dim NewString
k = 0
NewString = ""
For k = Ubound(arrOUList) to LBound(arrOUList) Step - 1
NewString = NewString & arrOUList(k) & "\"
Next
NewString = Left(NewString, Len(NewString) - 1)
Wscript.Echo NewString

Function Parse1(strDN)
Parse1 = Mid(strDN, InStr(strDN, "=") + 1)
Parse1 = Mid(Parse1, InStr(Parse1, "=") + 1)
Parse1 = MId(Parse1, 1, InStr(Parse1, "=") - 4)
End Function

Function Parse2(strDN)
Parse2 = Mid(strDN, InStr(strDN, "=") + 1)
Parse2 = Mid(Parse2, InStr(Parse2, "=") - 2)
Parse2 = Left(Parse2, InStr(UCase(Parse2), "DC=") - 2)
End Function

Function Parse3(strDN)
Parse3 = Mid(strDN, InStr(strDN, "=") + 1)
Parse3 = Mid(Parse3, InStr(Parse3, "=") - 2)
End Function

This will result in a string of: Restricted\Servers - OU\Windows 2000\MSCS, which is what I need.

Thanks again!

CluM09
 
not that i got your request right in the first place but nevermind, as a matter of interest you might find this neater and perhaps more robust


strUserDN = "CN=COMPUTERNAME,OU=MSCS,OU=Windows 2000,OU=Servers - OU,OU=Restricted,DC=MYDOMAIN,DC=COM"

Set objRootDSE = GetObject("LDAP://RootDSE")
strDNC = objRootDSE.Get("defaultNamingContext")
'strDNC = "DC=MYDOMAIN,DC=COM"
strUserDN = Left(strUserDN, Len(strUserDN) - Len(strDNC))

aArray = Split(strUserDN, ",")
For i = Ubound(aArray) - 1 to LBound(aArray) + 1 Step - 1
strTemp = Right(aArray(i), Len(aArray(i)) - InStr(aArray(i), "="))
strResult = strResult & strTemp & "\"
Next
strResult = Left(strResult, Len(strResult) - 1)
 
mrmovie,

Thank you for the response!

That is exactly what I need to do. The posted codes were just a part of the whole script I used to query the AD information and report it in a readable format.

Yes, your script is certainly a neater one to the overall script that I am trying to do. I query the AD for the SERVER object and parse each server for the information that I need to place in the report.

Another request I have is to parse the AD OU that contains the users that belong to a group and display the users by first name and last name. By default, the users will show with last name, first name.

What I need is to display the users that I get from the OU and display them with first name and last name order.

Below is my script:

InputGroup = InputBox ("Please enter a group name in the MYDOMAIN domain:","Domain User Info Script")
' Input box allows user to enter the username

strDomain = "MYDOMAIN"

'Connect to the domain
Set GroupObj = GetObject("WinNT://" & strDomain & "/" & InputGroup)

'Start of sort routine
Set DataList = CreateObject("ADODB.Recordset")
DataList.Fields.Append "strUserName", 200, 255 ' adVarChar
DataList.Open

' Getting the Users of a Group
For Each UserObj In GroupObj.Members
DataList.AddNew
DataList("strUserName") = UserObj.FullName
DataList.Update
Next

'Sort by user name
DataList.Sort = "strUserName"
DataList.MoveFirst
'Display output
Do Until DataList.EOF
strUserNameS = DataList.Fields.Item("strUserName")
Wscript.Echo Space(3) & strUserNameS
DataList.MoveNext
Loop

Thanks again!

CluM09
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top