Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

Text manipulation

Ok, I've got a list of users in the following format:

20051450:firstName lastName
20061579:firstName middleName lastName
...

The number before the name is the student ID number which will be the student's username.  I need to convert that list into a new list so I can use ldifde to import the users into AD on a Win2k server.  This is what the first entry above would need to look like after conversion:

dn: CN="firstName lastName",OU=Students,DC=prendergast,DC=com
changetype: add
cn: firstName lastName
objectclass: user
displayName: firstName lastName
givenName: firstName
name: firstName lastName
SAMAccountName: 20051450
sn: lastName
userPrincipalName: 20051450@prendergast.com

I've got over 900 users to enter, doing this by hand just isn't an option.  I had a PHP script that could do it before but don't have it anymore.  Any help is appreciated.  Thanks!
0
salvagbf
Asked:
salvagbf
1 Solution
 
ozgulkaanCommented:
open "c:\NAMEOFFILE.txt" for input as 1
open "c:\RESULTFILE.txt" for output as 2

do while not eof(1)
  line input #1,rowdata
  plc = instr(rowdata,":")

 
  dn: CN="firstName lastName",OU=Students,DC=prendergast,DC=com
  changetype: add
  cn: firstName lastName
  objectclass: user
  displayName: firstName lastName
  givenName: firstName
  name: firstName lastName
  SAMAccountName: 20051450
  sn: lastName
  userPrincipalName: 20051450@prendergast.com
loop
0
 
salvagbfAuthor Commented:
Thanks.. umm... how do I implement this?
0
 
b0bmasterCommented:
Here is a VBScript i made copy/paste it into notepad and save it as "Process.VBS"

------------------------------------------------------------------------------------------------------------------

  Set FSO= CreateObject("scripting.filesystemobject")
  Set InFile = FSO.GetFile("students.txt")
  Set OutFile = FSO.OpenTextFile("output.txt", 2, Not FSO.FileExists("output.txt"))
  Set TextStream = InFile.OpenAsTextStream(1)
 
   Do While Not TextStream.AtEndOfStream
   
    Line = TextStream.ReadLine
    ArrayX = Split(Line,":")
    StudentID = Trim(ArrayX(0))
    temp = Split(ArrayX(1)," ")
    StudentFirstName = Trim(temp(LBound(temp)))
    StudentSurName = Trim(temp(UBound(temp)))

    OutFile.Write "dn: CN=" + Chr(34) + StudentFirstName + " " + StudentSurName + Chr(34) + ",OU=Students,DC=prendergast,DC=com" & vbNewLine
    OutFile.Write "changetype: add" & vbNewLine
    OutFile.Write "cn: " + StudentFirstName + " " + StudentSurName & vbNewLine
    OutFile.Write "objectclass: user" & vbNewLine
    OutFile.Write "displayName: " + StudentFirstName + " " + StudentSurName & vbNewLine
    OutFile.Write "givenName: " + StudentFirstName & vbNewLine
    OutFile.Write "name: " + StudentFirstName + " " + StudentSurName & vbNewLine
    OutFile.Write "SAMAccountName: " + StudentID & vbNewLine
    OutFile.Write "sn: " + StudentSurName & vbNewLine
    OutFile.Write "userPrincipalName: " + StudentID + "@prendergast.com" & vbNewLine
    OutFile.Write vbNewLine
    OutFile.Write vbNewLine

   Loop

  TextStream.Close

---------------------------------------------------------------------------------------------------------------

Name the input file "students.txt" with the data as described in your question.
The output will be "output.txt"  

Thanks
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ozgulkaanCommented:
the below is tested with visual basic. but to use it as a vbscript it needs some changes.
put all the data in a text file, which is below NAMEOFFILE.txt
and run visual basic. it's done.

--------------------------------------------------------------------------------------------------

Open "c:\NAMEOFFILE.txt" For Input As 1
Open "c:\RESULTFILE.txt" For Output As 2

Do While Not EOF(1)
  Line Input #1, rowdata
  rowdata = Trim(rowdata)
  plc = InStr(rowdata, ":")
  StudentNo = Left(rowdata, plc - 1)
  rowdata = Right(rowdata, Len(rowdata) - plc)
  plc = InStr(rowdata, " ")
  StudentName = Left(rowdata, plc - 1)
  rowdata = Right(rowdata, Len(rowdata) - plc)
  plc = InStr(rowdata, " ")
  If plc = 0 Then 'if no middlename exists
    StudentLastName = rowdata
  Else
    StudentMiddleName = Left(rowdata, plc - 1)
    rowdata = Right(rowdata, Len(rowdata) - plc)
    StudentLastName = rowdata
  End If

  print#2, "dn: CN=""" & StudentName & " " & StudentLastName & """,OU=Students,DC=prendergast,DC=com"
  print#2, "changetype: add"
  print#2, "cn: " & StudentName & " " & StudentLastName
  print#2, "objectclass: user"
  print#2, "displayName: " & StudentName & " " & StudentLastName
  print#2, "givenName: " & StudentName
  print#2, "name: " & StudentName & " " & StudentLastName
  print#2, "SAMAccountName: " & StudentNo
  print#2, "sn: " & StudentLastName
  print#2, "userPrincipalName: " & StudentNo & "@prendergast.com"

Loop

close #1,2
0
 
salvagbfAuthor Commented:
Wow, awesome! Thank you both! I'm leaving for today but will test out your solutions and award points tomorrow, thank you both sooo much!
0
 
LittleRed1Commented:
Easier with a batch file and no dependencies.

Like this:

::-------------------------------------------------------------------------------------
@echo off

setlocal ENABLEDELAYEDEXPANSION

for /f "tokens=1-4 delims=: " %%a in (userlist.txt) do (
      if "%%d" == "" (
            :: Create temporary file if no middle name is present
            echo dn: CN="%%b %%c",OU=Students,DC=prendergast,DC=com>tmp.txt
            echo changetype: add>>tmp.txt
            echo cn: %%b %%c>>tmp.txt
            echo objectclass: user>>tmp.txt
            echo displayName: %%b %%c>>tmp.txt
            echo givenName: %%b>>tmp.txt
            echo name: %%b %%c>>tmp.txt
            echo SAMAccountName: %%a  >>tmp.txt
            echo sn: %%c>>tmp.txt
            echo userPrincipalName: %%a@prendergast.com>>tmp.txt
      ) else (
            :: Create temporary file if middle name is present, include middle name in name: field
            echo dn: CN="%%b %%d",OU=Students,DC=prendergast,DC=com>tmp.txt
            echo changetype: add>>tmp.txt
            echo cn: %%b %%d>>tmp.txt
            echo objectclass: user>>tmp.txt
            echo displayName: %%b %%d>>tmp.txt
            echo givenName: %%b>>tmp.txt
            echo name: %%b %%c %%d>>tmp.txt
            echo SAMAccountName: %%a  >>tmp.txt
            echo sn: %%d>>tmp.txt
            echo userPrincipalName: %%a@prendergast.com>>tmp.txt
      )
      
      :: Call LDIFDE here, add whatever additional switches you need.
      ldifde -i -f tmp.txt
)
::-------------------------------------------------------------------------------------
0
 
maxrizzatoCommented:
You can use gawk (GNU Awk is a batch text editor), found here, foe example:

http://unxutils.sourceforge.net

(together with a host of other *nix utils natively ported to Windows).

With awk there are many ways you can accomplish your goal, following is an example of the code you can use.

script.awk:

BEGIN{     FS="[: ]";
      termination = 1;
      getline;
      do{

            #FS="[: ]" sets colon and whitespace as field separators
            #getline reads a single line from input textfile
            #$0 is the entire line; $1 is the first field and so on.
            #$(NF) is the last field of the line, referenced using the internal variable NF that is set
            #to the number of fields in the line each time getline is called
            #as an example, $(i) can reference each field in the line,
            #if you make i cycle from 1 to NF

            printf("dn: CN=\"%s %s\",OU=Students,DC=prendergast,DC=com\n", $2, $(NF));  
            printf("changetype: add\n");
            printf("cn: %s %s\n", $2, $(NF));
            printf("objectclass: user\n");
            printf("displayName: %s %s\n", $2, $(NF));
            printf("givenName: %s\n", $2);
            printf("name: %s %s\n", $2, $(NF));
            printf("name: %s %s\n", $2, $(NF));                        
            printf("SAMAccountName: %s\n", $1);
            printf("sn: %s\n", $(NF));
            printf("userPrincipalName: %s@prendergast.com\n", $1);
            printf("\n");        #Just to separate stanzas. Comment it out if you don't want
                                            # a separation line

      }while(getline> 0)
}

You can invoke the above script from a cmd windows shell this way:

C:\>type inputfile.txt | gawk -f script.awk > output.txt

in output.txt you get the results.
0

Featured Post

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now