Can anyone help me please.

I am trying to split a users homedrive which I input using the
following code, the problem is I get an error when trying to split the
name. I basically want to get the servername and the home drive share
name as two seperate values so I can then use them seperatly.

The Home drive is displayed as "\\Servername\Sharename$"

username = "HJSimpson"


Const ADS_PROPERTY_DELETE = 4
Const ADS_PROPERTY_APPEND = 3

'Open connection to AD using LDAP
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection

strBase = "<LDAP://dc=name,dc=name>"

strFilter = "(&(objectCategory=person)(sAMAccountName=" & username &
"))"
strAttributes = "sAMAccountName,AdsPath,distinguishedName"
strQuery = strBase & ";" & strFilter & ";" & strAttributes
objCommand.CommandText = strQuery
Set ADSIRecordSet = objCommand.Execute
strName = ADSIRecordSet.Fields("sAMAccountName").Value
errorNum = err.number
errorDesc = err.description


strAdsPath = ADSIRecordSet.Fields("AdsPath").Value
strDN = ADSIRecordSet.Fields("distinguishedName").Value

Set objUser = GetObject("LDAP://" & strDN)

strDisplayName = objUser.displayname
strDescription = objUser.description

err.number = "0"

Set usr = GetObject(strAdsPath)

homedir = usr.homedirectory

MsgBox homedir - This bit is OK

a=Split(homedir,"\",-1,1) - This bit is not OK.

If InStr(a, "\\") Then
newservername = len(a,3)
Else
'Do Nothing
End If

MsgBox newservername

WScript.Quit:

Please help. Thanks

Re: Help Splitting a String by Joe

Joe
Wed Oct 24 04:48:14 PDT 2007

"meridean" <chris.john.flynn@gmail.com> wrote in message
news:1193222031.460419.29550@q5g2000prf.googlegroups.com...
> Can anyone help me please.
>
> I am trying to split a users homedrive which I input using the
> following code, the problem is I get an error when trying to split the
> name. I basically want to get the servername and the home drive share
> name as two seperate values so I can then use them seperatly.
>
> The Home drive is displayed as "\\Servername\Sharename$"
>
> username = "HJSimpson"
>
>
> Const ADS_PROPERTY_DELETE = 4
> Const ADS_PROPERTY_APPEND = 3
>
> 'Open connection to AD using LDAP
> Set objCommand = CreateObject("ADODB.Command")
> Set objConnection = CreateObject("ADODB.Connection")
> objConnection.Provider = "ADsDSOObject"
> objConnection.Open "Active Directory Provider"
> objCommand.ActiveConnection = objConnection
>
> strBase = "<LDAP://dc=name,dc=name>"
>
> strFilter = "(&(objectCategory=person)(sAMAccountName=" & username &
> "))"
> strAttributes = "sAMAccountName,AdsPath,distinguishedName"
> strQuery = strBase & ";" & strFilter & ";" & strAttributes
> objCommand.CommandText = strQuery
> Set ADSIRecordSet = objCommand.Execute
> strName = ADSIRecordSet.Fields("sAMAccountName").Value
> errorNum = err.number
> errorDesc = err.description
>
>
> strAdsPath = ADSIRecordSet.Fields("AdsPath").Value
> strDN = ADSIRecordSet.Fields("distinguishedName").Value
>
> Set objUser = GetObject("LDAP://" & strDN)
>
> strDisplayName = objUser.displayname
> strDescription = objUser.description
>
> err.number = "0"
>
> Set usr = GetObject(strAdsPath)
>
> homedir = usr.homedirectory
>
> MsgBox homedir - This bit is OK
>
> a=Split(homedir,"\",-1,1) - This bit is not OK.
>
> If InStr(a, "\\") Then
> newservername = len(a,3)
> Else
> 'Do Nothing
> End If
>
> MsgBox newservername
>
> WScript.Quit:
>
> Please help. Thanks
>
I'd use InStrRev to find the last instance of \.

--

Joe Fawcett (MVP - XML)

http://joe.fawcett.name



Re: Help Splitting a String by Tom

Tom
Wed Oct 24 06:02:00 PDT 2007

On Oct 24, 6:33 am, meridean <chris.john.fl...@gmail.com> wrote:
> Can anyone help me please.
>
> I am trying to split a users homedrive which I input using the
> following code, the problem is I get an error when trying to split the
> name. I basically want to get the servername and the home drive share
> name as two seperate values so I can then use them seperatly.
>
> The Home drive is displayed as "\\Servername\Sharename$"
>
> username = "HJSimpson"
>
> Const ADS_PROPERTY_DELETE = 4
> Const ADS_PROPERTY_APPEND = 3
>
> 'Open connection to AD using LDAP
> Set objCommand = CreateObject("ADODB.Command")
> Set objConnection = CreateObject("ADODB.Connection")
> objConnection.Provider = "ADsDSOObject"
> objConnection.Open "Active Directory Provider"
> objCommand.ActiveConnection = objConnection
>
> strBase = "<LDAP://dc=name,dc=name>"
>
> strFilter = "(&(objectCategory=person)(sAMAccountName=" & username &
> "))"
> strAttributes = "sAMAccountName,AdsPath,distinguishedName"
> strQuery = strBase & ";" & strFilter & ";" & strAttributes
> objCommand.CommandText = strQuery
> Set ADSIRecordSet = objCommand.Execute
> strName = ADSIRecordSet.Fields("sAMAccountName").Value
> errorNum = err.number
> errorDesc = err.description
>
> strAdsPath = ADSIRecordSet.Fields("AdsPath").Value
> strDN = ADSIRecordSet.Fields("distinguishedName").Value
>
> Set objUser = GetObject("LDAP://" & strDN)
>
> strDisplayName = objUser.displayname
> strDescription = objUser.description
>
> err.number = "0"
>
> Set usr = GetObject(strAdsPath)
>
> homedir = usr.homedirectory
>
> MsgBox homedir - This bit is OK
>
> a=Split(homedir,"\",-1,1) - This bit is not OK.
>
> If InStr(a, "\\") Then
> newservername = len(a,3)
> Else
> 'Do Nothing
> End If
>
> MsgBox newservername
>
> WScript.Quit:
>
> Please help. Thanks

I don't see that your problem is due to the line you indicate.
Rather, I see a problem with this bit of code ...

If InStr(a, "\\") Then
newservername = len(a,3)
Else
'Do Nothing
End If

because there should no longer be any backslashes present in the array
that was created by the Split() and you fail to treat 'a' as an
element in the array.

I believe you should find the server name in the array element a(2).
Elements a(0) and a(1) should be empty. Element a(3) should have the
sharepoint name.

I don't quite know what it is you are trying to do - create a new name
that only has the first three characters of the old name? - but from
your example, you might try something like this ...

If typename(a) = "Variant()" Then
newservername = len(a(2),3)
Else
msgbox "Error"
End If

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/


Re: Help Splitting a String by ekkehard

ekkehard
Wed Oct 24 12:22:13 PDT 2007

meridean schrieb:
> Can anyone help me please.
>
> I am trying to split a users homedrive which I input using the
> following code, the problem is I get an error when trying to split the
> name. I basically want to get the servername and the home drive share
> name as two seperate values so I can then use them seperatly.
>
> The Home drive is displayed as "\\Servername\Sharename$"
>
[...] LDAP code delete because not relevant for Split()

> homedir = usr.homedirectory
> MsgBox homedir - This bit is OK
>
> a=Split(homedir,"\",-1,1) - This bit is not OK.
> If InStr(a, "\\") Then
> newservername = len(a,3)
[...]
> Please help. Thanks
>
a (result of Split()) is an array; you can't use Instr() on it.
Len() takes a string and returns its length; to get the name part,
you should use something like
mid( a( 2 ), 3 )
if the "\\" is still prefixed (which it wouldn't after Split() on
"\".

I hope this code

Dim aTests : aTests = Array( _
Array( "\\Servername\Sharename$", "Servername" , "Sharename$" ) _
, Array( "no backslash in here" , "split on \ failed", "" ) _
, Array( "0\1\2" , "bad syntax" , "" ) _
, Array( "\\Servername\one\two$" , "Servername" , "one\two$" ) _
)

Dim aTest, aParts, bOk
For Each aTest In aTests
WScript.Echo "---", aTest( 0 )
aParts = Split( aTest( 0 ), "\" )
Select Case UBound( aParts )
Case 0 ' 1 item, split on \ failed
bOk = "split on \ failed" = aTest( 1 )
Case 1, 2
bOk = "bad syntax"
Case 3
bOk = aParts( 2 ) = aTest( 1 ) And aParts( 3 ) = aTest( 2 )
WScript.Echo " ", aParts( 2 ), aParts( 3 )
Case Else
Dim nIdx
For nIdx = 4 To UBound( aParts )
aParts( 3 ) = aParts( 3 ) & "\" & aParts( nIdx )
Next
bOk = aParts( 2 ) = aTest( 1 ) And aParts( 3 ) = aTest( 2 )
WScript.Echo " ", aParts( 2 ), aParts( 3 )
End Select
WScript.Echo " ", CStr( bOk )

Next

--------- output -------
=== splitHomeDir: split homedir =======
--- \\Servername\Sharename$
Servername Sharename$
Wahr
--- no backslash in here
Wahr
--- 0\1\2
bad syntax
--- \\Servername\one\two$
Servername one\two$
Wahr
=== splitHomeDir: 0 done (00:00:00) ===

will help you to understand/experiment with Split() on a HomeDir.

Re: Help Splitting a String by meridean

meridean
Thu Oct 25 00:49:38 PDT 2007

On Oct 24, 2:02 pm, Tom Lavedas <tglba...@cox.net> wrote:
> On Oct 24, 6:33 am, meridean <chris.john.fl...@gmail.com> wrote:
>
>
>
>
>
> > Can anyone help me please.
>
> > I am trying to split a users homedrive which I input using the
> > following code, the problem is I get an error when trying to split the
> > name. I basically want to get the servername and the home drive share
> > name as two seperate values so I can then use them seperatly.
>
> > The Home drive is displayed as "\\Servername\Sharename$"
>
> > username = "HJSimpson"
>
> > Const ADS_PROPERTY_DELETE = 4
> > Const ADS_PROPERTY_APPEND = 3
>
> > 'Open connection to AD using LDAP
> > Set objCommand = CreateObject("ADODB.Command")
> > Set objConnection = CreateObject("ADODB.Connection")
> > objConnection.Provider = "ADsDSOObject"
> > objConnection.Open "Active Directory Provider"
> > objCommand.ActiveConnection = objConnection
>
> > strBase = "<LDAP://dc=name,dc=name>"
>
> > strFilter = "(&(objectCategory=person)(sAMAccountName=" & username &
> > "))"
> > strAttributes = "sAMAccountName,AdsPath,distinguishedName"
> > strQuery = strBase & ";" & strFilter & ";" & strAttributes
> > objCommand.CommandText = strQuery
> > Set ADSIRecordSet = objCommand.Execute
> > strName = ADSIRecordSet.Fields("sAMAccountName").Value
> > errorNum = err.number
> > errorDesc = err.description
>
> > strAdsPath = ADSIRecordSet.Fields("AdsPath").Value
> > strDN = ADSIRecordSet.Fields("distinguishedName").Value
>
> > Set objUser = GetObject("LDAP://" & strDN)
>
> > strDisplayName = objUser.displayname
> > strDescription = objUser.description
>
> > err.number = "0"
>
> > Set usr = GetObject(strAdsPath)
>
> > homedir = usr.homedirectory
>
> > MsgBox homedir - This bit is OK
>
> > a=Split(homedir,"\",-1,1) - This bit is not OK.
>
> > If InStr(a, "\\") Then
> > newservername = len(a,3)
> > Else
> > 'Do Nothing
> > End If
>
> > MsgBox newservername
>
> > WScript.Quit:
>
> > Please help. Thanks
>
> I don't see that your problem is due to the line you indicate.
> Rather, I see a problem with this bit of code ...
>
> If InStr(a, "\\") Then
> newservername = len(a,3)
> Else
> 'Do Nothing
> End If
>
> because there should no longer be any backslashes present in the array
> that was created by the Split() and you fail to treat 'a' as an
> element in the array.
>
> I believe you should find the server name in the array element a(2).
> Elements a(0) and a(1) should be empty. Element a(3) should have the
> sharepoint name.
>
> I don't quite know what it is you are trying to do - create a new name
> that only has the first three characters of the old name? - but from
> your example, you might try something like this ...
>
> If typename(a) = "Variant()" Then
> newservername = len(a(2),3)
> Else
> msgbox "Error"
> End If
>
> Tom Lavedas
> ===========http://members.cox.net/tglbatch/wsh/- Hide quoted text -
>
> - Show quoted text -

Tom thanks for this, I now understand what I was doing wrong. I have
now amended my script, taken out the If statement and used the array
correctly and now I have the split complete just as I was wanting.
Thanks for your help.


Re: Help Splitting a String by meridean

meridean
Thu Oct 25 00:51:20 PDT 2007

On Oct 24, 8:22 pm, "ekkehard.horner" <ekkehard.hor...@arcor.de>
wrote:
> meridean schrieb:> Can anyone help me please.
>
> > I am trying to split a users homedrive which I input using the
> > following code, the problem is I get an error when trying to split the
> > name. I basically want to get the servername and the home drive share
> > name as two seperate values so I can then use them seperatly.
>
> > The Home drive is displayed as "\\Servername\Sharename$"
>
> [...] LDAP code delete because not relevant for Split()
>
> > homedir = usr.homedirectory
> > MsgBox homedir - This bit is OK
>
> > a=Split(homedir,"\",-1,1) - This bit is not OK.
> > If InStr(a, "\\") Then
> > newservername = len(a,3)
> [...]
> > Please help. Thanks
>
> a (result of Split()) is an array; you can't use Instr() on it.
> Len() takes a string and returns its length; to get the name part,
> you should use something like
> mid( a( 2 ), 3 )
> if the "\\" is still prefixed (which it wouldn't after Split() on
> "\".
>
> I hope this code
>
> Dim aTests : aTests = Array( _
> Array( "\\Servername\Sharename$", "Servername" , "Sharename$" ) _
> , Array( "no backslash in here" , "split on \ failed", "" ) _
> , Array( "0\1\2" , "bad syntax" , "" ) _
> , Array( "\\Servername\one\two$" , "Servername" , "one\two$" ) _
> )
>
> Dim aTest, aParts, bOk
> For Each aTest In aTests
> WScript.Echo "---", aTest( 0 )
> aParts = Split( aTest( 0 ), "\" )
> Select Case UBound( aParts )
> Case 0 ' 1 item, split on \ failed
> bOk = "split on \ failed" = aTest( 1 )
> Case 1, 2
> bOk = "bad syntax"
> Case 3
> bOk = aParts( 2 ) = aTest( 1 ) And aParts( 3 ) = aTest( 2 )
> WScript.Echo " ", aParts( 2 ), aParts( 3 )
> Case Else
> Dim nIdx
> For nIdx = 4 To UBound( aParts )
> aParts( 3 ) = aParts( 3 ) & "\" & aParts( nIdx )
> Next
> bOk = aParts( 2 ) = aTest( 1 ) And aParts( 3 ) = aTest( 2 )
> WScript.Echo " ", aParts( 2 ), aParts( 3 )
> End Select
> WScript.Echo " ", CStr( bOk )
>
> Next
>
> --------- output -------
> === splitHomeDir: split homedir =======
> --- \\Servername\Sharename$
> Servername Sharename$
> Wahr
> --- no backslash in here
> Wahr
> --- 0\1\2
> bad syntax
> --- \\Servername\one\two$
> Servername one\two$
> Wahr
> === splitHomeDir: 0 done (00:00:00) ===
>
> will help you to understand/experiment with Split() on a HomeDir.

Thank you for your help and suggestions. As above I have used the
information and corrected my existing code and it all works fine now.
Many Thanks.