?
Solved

Reformat exported file?

Posted on 2014-07-14
11
Medium Priority
?
268 Views
Last Modified: 2014-07-16
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;Lastname;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)
0
Comment
Question by:mathiesen-data
  • 5
  • 4
  • 2
11 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 40194294
After this, you can use the variables strUserName, strFullName, strFirstName, strLastName, strPassword however you need them:
strTextline = objTextFile.Readline
If inStr(strTextline, ";") > 0 Then
	arrUsers = Split(strTextline, ";")
	strUserName = arrUsers(0)
	strFullName = arrUsers(1)
	strPassword = arrUsers(2)
	arrNameComponents = Split(strFullname, " ")
	strLastName = arrNameComponents(UBound(arrNameComponents))
	Redim Preserve arrNameComponents(UBound(arrNameComponents) - 1)
	strFirstName = Join(arrNameComponents, " ")
	Wscript.Echo "Username:   " & strUserName
	Wscript.Echo "Full name:  " & strFullName
	Wscript.Echo "First name: " & strFirstName
	Wscript.Echo "Last name:  " & strLastName
	Wscript.Echo "Password:   " & strPassword
End If

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40194301
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

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

Open in new window


[Edit: was bit slow posting there... similar to oBdA but messier, was quick!]
0
 
LVL 2

Author Comment

by:mathiesen-data
ID: 40194373
Shouldnt these 3 lines:
Wscript.Echo "Lastname: " & LastName
Wscript.Echo "Firstname: " & FirstName
Wscript.Echo "Password: " & arrUsers(2)

Open in new window


look like this to fit my existing script?
Wscript.Echo "Lastname: " & arrUsers(1)
Wscript.Echo "Firstname: " & arrUsers(2)
Wscript.Echo "Password: " & arrUsers(3)

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

by:mathiesen-data
ID: 40194384
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)
0
 
LVL 85

Expert Comment

by:oBdA
ID: 40194398
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":
Username:   Username
Full name:  Firstname MiddleName Lastname
First name: Firstname MiddleName
Last name:  Lastname
Password:   Password

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40194417
When you were referring to my option, the entry

Wscript.Echo "Lastname: " & LastName
Wscript.Echo "Firstname: " & FirstName
Wscript.Echo "Password: " & arrUsers(2)

Open in new window


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
0
 
LVL 2

Author Comment

by:mathiesen-data
ID: 40194552
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?
0
 
LVL 85

Expert Comment

by:oBdA
ID: 40194585
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;Lastname;Password":
arrUsers(0) --> strUserName
arrUsers(1) --> strFirstName
arrUsers(2) --> strLastName
arrUsers(3) --> strPassword
0
 
LVL 2

Author Comment

by:mathiesen-data
ID: 40199095
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
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 40199118
From what I can see (without being able to test it), all you need to do is remove line 32 ("End If"), and adjust the indentation accordingly. Line 32 closes "If inStr(strTextline, ";") > 0 Then" from line 18, but it should continue until the "End If" line 87.
0
 
LVL 2

Author Comment

by:mathiesen-data
ID: 40199644
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:

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")

Open in new window

Createusers.vbs
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month16 days, 22 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question