Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract info from text file & create new file 1

Status
Not open for further replies.

tooltime63

Vendor
Joined
Apr 19, 2005
Messages
3
Location
US
I'm very new to VBScript but need to perform the following function. Extract certain values from one file & create new csv file. Source file content:

dn: CN=Ben,CN=Users,DC=AXSP2,DC=local
changetype: add
title: Engineer
department: Information System Division
userPrincipalName: ben@AXSP2.local
manager: CN=John,CN=Users,DC=AXSP2,DC=local

dn: CN=Brenda,CN=Users,DC=AXSP2,DC=local
changetype: add
title: Executive Director
department: Ofc of Admin Services
userPrincipalName: Brenda@AXSP2.local
manager: CN=Butch,CN=Users,DC=AXSP2,DC=local

New file format needed:

Ben,Information System Division,Engineer,John
Brenda,Ofc of Admin Services,Executive Director,Butch
 
This may not be the most efficient, but you can give it a try.

Code:
Option Explicit
'On Error Resume Next

Dim objFSO, inputFile, outputFile, objInFile, objOutFile, strLine
Dim arrOutput(3), arrTemp, arrTemp2

Const ForReading = 1, ForWriting = 2, ForAppending = 8

inputFile = "c:\temp\input.txt"
outputFile = "c:\temp\output.csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInFile = objFSO.OpenTextFile(inputFile, ForReading)
Set objOutFile = objFSO.OpenTextFile(outputFile, ForWriting, True)

Do Until objInFile.AtEndOfStream
    strLine = objInFile.ReadLine
    If Not strLine = "" Or IsNull(strLine) Then
        Select Case UCase(Left(strLine, InStr(strLine, ":") - 1))
            Case "DN"
                arrTemp = Split(strLine)
                arrTemp2 = Split(arrTemp(1), ",")
                arrOutput(0) = Replace(arrTemp2(0), "CN=", "")
            Case "DEPARTMENT"
                arrOutput(1) = Mid(strLine, InStr(strLine, ":") + 2)
            Case "TITLE"
                arrOutput(2) = Mid(strLine, InStr(strLine, ":") + 2)
            Case "MANAGER"
                arrTemp = Split(strLine)
                arrTemp2 = Split(arrTemp(1), ",")
                arrOutput(3) = Replace(arrTemp2(0), "CN=", "")
                objOutFile.WriteLine Join(arrOutput, ",")
        End Select
    End If
Loop

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Perfect!!! Thank you so much! That was awesome!
 
dm4ever this is an excellent example of how to use arrays and instring line splitting. Thanks very much I believe I can use this to help sovle a problem I have with a customer supplied report.

And you provided them with a complete solution.

That deserves a star..

Thanks !!!!!

Thanks

John Fuhrman
Titan Global Services
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top