• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8454
  • Last Modified:

VBscript code to skip first line in text file

I need to put code into the attached script to skip the first line of the input text file.  I've tried this tip (http://www.experts-exchange.com/Programming/Languages/Scripting/Q_23053739.html), but my output file contains no data.  What is the correct code that needs to be put into the attached script?
'AddSpridenID.vbs
'Written by Jason Loomans
'This script will take the iSeries student ID on an account, find the corresponding AD account, and add the SPRIDEN ID to the AD account.
 
'Variables
Dim objFSO, arrStr, User
 
strInputFile = WScript.Arguments.Item(0)
strOutputFile = WScript.Arguments.Item(1)
Const StaffOU = ",<ldappathhere>"
 
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
 
'strMsgboxVerify = MsgBox("You are about to add the Banner SPRIDEN ID to all accounts in Active Directory.  Continue?",20,"VBScript")
'If strMsgboxVerify = 7 Then 
'	Wscript.Echo("Script terminated")
'	Wscript.Quit(1)
'	End if
 
'Create new data file
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set OutFile = objFSO.CreatetextFile(strOutputFile, True)
OutFile.WriteLine "Action,Type,Name,EmailAddress,ForceChangePassword,Password,DisplayName,FirstName,LastName"
OutFile.Close
Set OutFile1 = objFSO.OpenTextFile(strOutputFile,ForAppending,True)
 
'Open Banner Extract, read SAMAccountName, retrieve, and write correct info to new file.
Set objDataFile = objFSO.OpenTextFile(strInputFile,ForReading)
On Error Resume Next
Do until objDataFile.AtEndofStream
	arrStr = Split(objDataFile.ReadLine,",")
	strSAMAcctName = arrStr(2)
	strEmailAddress = arrStr(3)
	strDisplayName = arrStr(6)
	strFirstName = arrStr(7)
	strLastName = arrStr(8)
 
	Set User = GetObject ("LDAP://CN=" & strSAMAcctName & StudentOU)
 
				Password = Left(User.Lastname,3) & Right(User.adminDisplayName,5)
				Password = Ucase(Password)
				PasswordA = Left(User.Lastname,3) & User.physicalDeliveryOfficeName
				PasswordA = Ucase(PasswordA)
 
				OutFile1.WriteLine "Update,Mailbox," & strSAMAcctName & "," & strEmailAddress & ",1," & PasswordA & "," & strDisplayName & "," & strFirstName & "," & strLastName
         
Loop
 
objDataFile.Close
OutFile1.Close
 
'Delete original file and rename new file to original name
objFSO.DeleteFile strInputFile
objFSO.MoveFile strOutputFile, strInputFile
 
set objFSO = nothing
 
'strMsgboxEnd = Msgbox("Finished!  Terminating script.",64,"VBScript")

Open in new window

0
jloomans
Asked:
jloomans
  • 9
  • 8
  • 3
1 Solution
 
David LeeCommented:
Hi, jloomans.

Do a objDataFile.ReadLine before entering the Do Until loop on line 32.  
0
 
jloomansAuthor Commented:
Unfortunately this isn't working as intended.  The data I need returned from the script in the output file does not present itself in the destination file.
0
 
David LeeCommented:
Sorry, but I don't understand.  
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jloomansAuthor Commented:
Essentially what I am doing is I am inputting a source CSV file which needs to be normalized by through LDAP calls.  The output is put into a new file.  The first line of the source CSV file contains a header row, which needs to be skipped.  The data that requires normalization starts on the second line.  Using the code you advised, it broke the script and no output was returned.  

Essentially, what I need to do is skip the first line of the sources CSV file.

Hope this helps. :)
0
 
David LeeCommented:
"Using the code you advised, it broke the script and no output was returned. "

Well, I don't know how that's possible.  Not doubting that it is, but it doesn't make sense.  Assuming that the file is composed of multiple lines, then I don't understand how reading the first line can cause all the rest of the reads to somehow fail.  Unless the file is not composed of individual lines.  Try this, insert this sequence of code immediately before line #32.

varBuffer = objDataFile.ReadLine
WScript.Echo varBuffer

This will read the first line, then display it in a popup dialog-box.  Does the dialog-box show just the header information?
0
 
rejoinderCommented:
Inset this into line 32;
objDataFile.SkipLine

The block of code nearby should read;
On Error Resume Next
objDataFile.SkipLine
Do until objDataFile.AtEndofStream

Can you try this to see if you get the desired results.
0
 
jloomansAuthor Commented:
No luck on both suggestions.  The script breaks after line 32 when inserting the code.

This code displayed the header line of the file:

varBuffer = objDataFile.ReadLine
Wscript.echo varBuffer
0
 
rejoinderCommented:
Here is another attempt.  Can you replace these lines in the code.
When you are typing in the command line, can you please provide an example of what it is you are typing.  I want to know if perhaps there are spaces in the file name or path and the script is getting hung up on that.

On Error Resume Next
Do until objDataFile.AtEndofStream
        objDataFile.Skipline
        strLine = objDataFile.ReadLine
	arrStr = Split(strLine,",")

Open in new window

0
 
jloomansAuthor Commented:
The command line I am inputting is:

cscript.exe correctdata.vbs DeltaImport.csv temp.csv

Putting in the code helped the script complete, but the data didn't populate the destination file correctly.  I don't think split is the correct operation to use.

0
 
rejoinderCommented:
Can you please post a sample line from the inpput file?
0
 
jloomansAuthor Commented:
Action,Type,Name,EmailAddress,ForceChangePassword,Password,DisplayName,FirstName,LastName
Update,Mailbox,MyName,MyEmail@Address,1,MyPassword,MyDisplayName,MyFirstName,MyLastName
0
 
rejoinderCommented:
Just a thought... do your display names have commas in them?
0
 
rejoinderCommented:
Or... do you have spaces before or after the commas?  If you do, you will have to use th trim command to drop off the extra spaces from the arrays.

I also see that there may be a comma missing from the getobject command line.  Try this in place of what's there;
On Error Resume Next
Do until objDataFile.AtEndofStream
        objDataFile.Skipline
        strLine         = objDataFile.ReadLine
      arrStr          = Split(strLine,",")
      strSAMAcctName  = trim(arrStr(2))
      strEmailAddress = trim(arrStr(3))
      strDisplayName  = trim(arrStr(6))
      strFirstName    = trim(arrStr(7))
      strLastName     = trim(arrStr(8))
 
      Set User = GetObject ("LDAP://CN=" & strSAMAcctName & "," & StudentOU)
0
 
rejoinderCommented:
One more note here, you could comment out the on error resume next and it might show what caused the script to fail and not write to the log file.
0
 
jloomansAuthor Commented:
Still no luck in the latest round of testing.  Commenting out the "on error resume next" yielded no error message. There are no commas in any data in the source data file.   In addition, the comma for the LDAP call that was mentions is part of the variable to supply the OU.
0
 
rejoinderCommented:
In line 40 of your original script;
Set User = GetObject ("LDAP://CN=" & strSAMAcctName & StudentOU)
Where does the variable StudentOU get its value?
0
 
jloomansAuthor Commented:
Its constructed at the top of the script.  

Const StudentOU = ",OU=Students,OU=Accounts,DC=My,DC=Domain"
0
 
rejoinderCommented:
I think I might know what's going on.  You are trying to pass the users login ID into the ADSPath, the CN=... section.  You need to use the display name in the ADSPath.  In AD, click View, Advanced.  Right click a student and select properties.  Click the object tab.  Look at the canonical name of the student.  Does the students name match what is in your text file under the name field?
For example; the student's canonical name in AD looks like this my.domain /Accounts/Students/John Doe
In your text file, are you passing the login name so that if you were to see the full CN it would look like this  "CN=john.doe,OU=Students,OU=Accounts,DC=my,DC=domain"?
0
 
jloomansAuthor Commented:
Unfortunately not.  Our canonical names are set to be the same as the SAM account name, first initial and last name.


PS - Sorry for late response.  I was out yesterday afternoon.
0
 
rejoinderCommented:
Please try this version of the script...
'AddSpridenID.vbs
'Written by Jason Loomans
'This script will take the iSeries student ID on an account, find the corresponding AD account, and add the SPRIDEN ID to the AD account.
 
'Variables
Dim objFSO, arrStr, User
 
strInputFile = WScript.Arguments.Item(0)
strOutputFile = WScript.Arguments.Item(1)
Const StaffOU = ",<ldappathhere>"
Const StudentOU = ",OU=Students,OU=Accounts,DC=My,DC=Domain"
 
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
 
'strMsgboxVerify = MsgBox("You are about to add the Banner SPRIDEN ID to all accounts in Active Directory.  Continue?",20,"VBScript")
'If strMsgboxVerify = 7 Then 
'	Wscript.Echo("Script terminated")
'	Wscript.Quit(1)
'	End if
 
'Create new data file
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set OutFile = objFSO.CreatetextFile(strOutputFile, True)
OutFile.WriteLine "Action,Type,Name,EmailAddress,ForceChangePassword,Password,DisplayName,FirstName,LastName"
 
'Open Banner Extract, read SAMAccountName, retrieve, and write correct info to new file.
Set objDataFile = objFSO.OpenTextFile(strInputFile,ForReading)
'On Error Resume Next
objDataFile.Skipline
Do until objDataFile.AtEndofStream
	strLine         = objDataFile.ReadLine
	arrStr          = Split(strLine,",")
	strSAMAcctName  = trim(arrStr(2))
	strEmailAddress = trim(arrStr(3))
	strDisplayName  = trim(arrStr(6))
	strFirstName    = trim(arrStr(7))
	strLastName     = trim(arrStr(8))
	strLDAP = "LDAP://CN=" & strSAMAcctName & StudentOU
	Set User = GetObject (strLDAP)
	Password = Left(User.Lastname,3) & Right(User.adminDisplayName,5)
	Password = Ucase(Password)
	PasswordA = Left(User.Lastname,3) & User.physicalDeliveryOfficeName
	PasswordA = Ucase(PasswordA)
 	strWriteLine = "Update,Mailbox," & strSAMAcctName & "," & strEmailAddress & ",1," & PasswordA & "," & strDisplayName & "," & strFirstName & "," & strLastName
	OutFile.WriteLine strWriteLine
Loop
 
objDataFile.Close
OutFile.Close
 
Delete original file and rename new file to original name
objFSO.DeleteFile strInputFile
objFSO.MoveFile strOutputFile, strInputFile
 
set objFSO = nothing
 
'strMsgboxEnd = Msgbox("Finished!  Terminating script.",64,"VBScript")

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 9
  • 8
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now