mathiesen-data
asked on
Reformat exported file?
I have a script that creates users from a file received from an external source.
It looks like this
Username;Firstname Lastname;Password
I need to split the lastname from the firstname. It should end up being used in the below statement.
Username;Firstname;Lastnam e;Password
Ofcourse if you somehow can change the below statement to import directly without changing the source file directly that would be even better.
Users can have a middle name too, and that should be treated as a first name like so:Firstname Middelname; Lastname.
strTextline = objTextFile.Readline
If inStr(strTextline, ";") > 0 Then
arrUsers = split(strTextline, ";")
'Wscript.Echo "Username: " & arrUsers(0)
'Wscript.Echo "Lastname: " & arrUsers(1)
'Wscript.Echo "Firstname: " & arrUsers(2)
'Wscript.Echo "Password: " & arrUsers(3)
It looks like this
Username;Firstname Lastname;Password
I need to split the lastname from the firstname. It should end up being used in the below statement.
Username;Firstname;Lastnam
Ofcourse if you somehow can change the below statement to import directly without changing the source file directly that would be even better.
Users can have a middle name too, and that should be treated as a first name like so:Firstname Middelname; Lastname.
strTextline = objTextFile.Readline
If inStr(strTextline, ";") > 0 Then
arrUsers = split(strTextline, ";")
'Wscript.Echo "Username: " & arrUsers(0)
'Wscript.Echo "Lastname: " & arrUsers(1)
'Wscript.Echo "Firstname: " & arrUsers(2)
'Wscript.Echo "Password: " & arrUsers(3)
Try this to split at the " " too... can be made more fool proof. If there are more than one spaces in the name then it take the last word as the surname as the first two words as the first name as it stands
Steve
[Edit: was bit slow posting there... similar to oBdA but messier, was quick!]
Steve
strTextline = objTextFile.Readline
If inStr(strTextline, ";") > 0 Then
' Split the full name field into first and last names...
arrUsers = split(strTextline, ";")
if instr(arrusers(1)," ") > 0 then
arrName=split(arrUsers(1)," ")
if ubound(arrName)>1 then
LastName=arrName(ubound(arrName))
FirstName=arrName(0) + " " + arrName(1)
else
LastName=arrName(1)
FirstName=arrName(0)
else
LastName=arrUsers(0)
FirstName=""
end if
Wscript.Echo "Username: " & arrUsers(0)
Wscript.Echo "Lastname: " & LastName
Wscript.Echo "Firstname: " & FirstName
Wscript.Echo "Password: " & arrUsers(2)
end if
[Edit: was bit slow posting there... similar to oBdA but messier, was quick!]
ASKER
Shouldnt these 3 lines:
look like this to fit my existing script?
Wscript.Echo "Lastname: " & LastName
Wscript.Echo "Firstname: " & FirstName
Wscript.Echo "Password: " & arrUsers(2)
look like this to fit my existing script?
Wscript.Echo "Lastname: " & arrUsers(1)
Wscript.Echo "Firstname: " & arrUsers(2)
Wscript.Echo "Password: " & arrUsers(3)
ASKER
I am getting confused here. Does the first post also take into account several names in first name?
The rest of my script uses arrUsers(0) to arrUsers(2)
The rest of my script uses arrUsers(0) to arrUsers(2)
Yep. It reads the line and breaks it down into variables that re named after their use.
Output with strTextline = "Username;Firstname MiddleName Lastname;Password":
Output with strTextline = "Username;Firstname MiddleName Lastname;Password":
Username: Username
Full name: Firstname MiddleName Lastname
First name: Firstname MiddleName
Last name: Lastname
Password: Password
When you were referring to my option, the entry
is correct. Mine just splits the name field - arrUsers(1) - into Firstname and LastName. So:
arrUsers(0) = first field
arrUsers(1) = second field - split into First Name and LastName
arrUsers(2) = third field
The split command splits the line up at, in this case, the ; into different array entries
I then split this up using spaces so you can use.
The other one is neater though :-)
Steve
Wscript.Echo "Lastname: " & LastName
Wscript.Echo "Firstname: " & FirstName
Wscript.Echo "Password: " & arrUsers(2)
is correct. Mine just splits the name field - arrUsers(1) - into Firstname and LastName. So:
arrUsers(0) = first field
arrUsers(1) = second field - split into First Name and LastName
arrUsers(2) = third field
The split command splits the line up at, in this case, the ; into different array entries
I then split this up using spaces so you can use.
The other one is neater though :-)
Steve
ASKER
oBdA, can I just put the code above into the rest of the script and still refer to the
arrUsers(0) - (3) in the rest of my original script?
arrUsers(0) - (3) in the rest of my original script?
Currently no. It could certainly be changed by redefining the arrUsers array accordingly, but ...
1. It makes the script a lot less error prone and easier to read when you use, for example, "strFirstName" instead of "arrUsers(2)" (which is why I used strFullName = arrUsers(1) ... arrNameComponents = Split(strFullname, " ")) instead of arrNameComponents = Split(arrUsers(1), " "))
2. If you ever need to change the input format again, all you need to do is change the few lines that define the "readable" variables, instead of having to search and replace arrUsers(x) all over the place.
In other words: the best solution would be to do a search and replace like this (except obviously for lines 4-6 in my above script), based on your format "Username;Firstname;Lastna me;Passwor d":
arrUsers(0) --> strUserName
arrUsers(1) --> strFirstName
arrUsers(2) --> strLastName
arrUsers(3) --> strPassword
1. It makes the script a lot less error prone and easier to read when you use, for example, "strFirstName" instead of "arrUsers(2)" (which is why I used strFullName = arrUsers(1) ... arrNameComponents = Split(strFullname, " ")) instead of arrNameComponents = Split(arrUsers(1), " "))
2. If you ever need to change the input format again, all you need to do is change the few lines that define the "readable" variables, instead of having to search and replace arrUsers(x) all over the place.
In other words: the best solution would be to do a search and replace like this (except obviously for lines 4-6 in my above script), based on your format "Username;Firstname;Lastna
arrUsers(0) --> strUserName
arrUsers(1) --> strFirstName
arrUsers(2) --> strLastName
arrUsers(3) --> strPassword
ASKER
I tried editing my script, but now I get an error.
Line: 87
Char: 1
Error : Expected Statement
Code: 800A0400
Oprettelse-af-Brugere-ArrUsers-edit.vbs
Line: 87
Char: 1
Error : Expected Statement
Code: 800A0400
Oprettelse-af-Brugere-ArrUsers-edit.vbs
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect, it worked.
I attached the final script. It didnt create the user folders, so I had to add some more scripting for that to happen.
KGDC01 is the domain controller.
Below the extra stuff I did:
I attached the final script. It didnt create the user folders, so I had to add some more scripting for that to happen.
KGDC01 is the domain controller.
Below the extra stuff I did:
Set wshShell = CreateObject("Wscript.Shell")
'************************************************
'* CREATE USER DIRECTORY *
'************************************************
strShareName = strUserName
Set objFolder = objFSO.CreateFolder("\\kgdc01\Users\" & strShareName)
'************************************************
'* Set Permissions on USER DIRECTORY *
'************************************************
return = wshShell.Run ("icacls \\kgdc01\Users\" & strShareName & " " & "/grant " & strUserName & ":(OI)(CI)F")
Createusers.vbs
Open in new window