[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBscript code to skip first line in text file

Posted on 2008-11-07
20
Medium Priority
?
8,092 Views
Last Modified: 2012-05-05
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
Comment
Question by:jloomans
  • 9
  • 8
  • 3
20 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 22907826
Hi, jloomans.

Do a objDataFile.ReadLine before entering the Do Until loop on line 32.  
0
 

Author Comment

by:jloomans
ID: 22908073
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
 
LVL 76

Expert Comment

by:David Lee
ID: 22909012
Sorry, but I don't understand.  
0
Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

 

Author Comment

by:jloomans
ID: 22909109
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
 
LVL 76

Expert Comment

by:David Lee
ID: 22909208
"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
 
LVL 14

Expert Comment

by:rejoinder
ID: 22909546
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
 

Author Comment

by:jloomans
ID: 22909648
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
 
LVL 14

Expert Comment

by:rejoinder
ID: 22909675
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
 

Author Comment

by:jloomans
ID: 22910083
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
 
LVL 14

Expert Comment

by:rejoinder
ID: 22912859
Can you please post a sample line from the inpput file?
0
 

Author Comment

by:jloomans
ID: 22912996
Action,Type,Name,EmailAddress,ForceChangePassword,Password,DisplayName,FirstName,LastName
Update,Mailbox,MyName,MyEmail@Address,1,MyPassword,MyDisplayName,MyFirstName,MyLastName
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 22913169
Just a thought... do your display names have commas in them?
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 22913185
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
 
LVL 14

Expert Comment

by:rejoinder
ID: 22913190
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
 

Author Comment

by:jloomans
ID: 22922705
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
 
LVL 14

Expert Comment

by:rejoinder
ID: 22923672
In line 40 of your original script;
Set User = GetObject ("LDAP://CN=" & strSAMAcctName & StudentOU)
Where does the variable StudentOU get its value?
0
 

Author Comment

by:jloomans
ID: 22924199
Its constructed at the top of the script.  

Const StudentOU = ",OU=Students,OU=Accounts,DC=My,DC=Domain"
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 22924945
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
 

Author Comment

by:jloomans
ID: 22930729
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
 
LVL 14

Accepted Solution

by:
rejoinder earned 1500 total points
ID: 22957589
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

867 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