Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: script to extract data from text file and put into excel format

  1. #1
    tdubb Guest

    script to extract data from text file and put into excel format

    Is there a script that will extract data from text file and put it into
    exfel column/rows format?



  2. #2
    Pegasus \(MVP\) Guest

    Re: script to extract data from text file and put into excel format

    Yes, there is.

    I case you also wanted to know how it's done, have a look at this
    example: http://www.windowsitlibrary.com/Content/1258/09/2.html

  3. #3
    tdubb Guest

    Re: script to extract data from text file and put into excel format

    Here is my text file

    =======================================================

    Category: 2 string Logon/Logoff
    ComputerName: Server
    Logfile: Security source Security
    EventCode: 528
    EventType: 4
    Type: Audit Success
    User: domain\username
    Message: Successful Logon:

    User Name: username
    Domain: domain
    Logon ID: (0x0,0x245D6D8)
    Logon Type: 10
    Logon Process: User32
    Authentication Package: Negotiate
    Workstation Name: Server
    Logon GUID: {6bf7409a-dc43-e893-6355-dcf937334df5}
    Caller User Name: server$
    Caller Domain: domain
    Caller Logon ID: (0x0,0x3E7)
    Caller Process ID: 4320
    Transited Services: -
    Source Network Address: x.x.x.x
    Source Port: 46563
    .............


    ============================================================

    there are probably 100's or 1000's of these in a text file and I want to put

    the computerName, Logfile, Event code, Username, Domain, etc etc..... into
    excel colums and rows


    any idea?

  4. #4
    Pegasus \(MVP\) Guest

    Re: script to extract data from text file and put into excel format

    You could base your script on the example in the link
    that I gave you in my first reply, or you could write out
    each row as a line of text, using tabs to separate the
    various fields from each other. Excel can easily open
    tab-delimited files.

  5. #5
    tony Guest

    Re: script to extract data from text file and put into excel format

    I dont understand how to do to this

    hope someone can help

    Basically if I had a text file

    first name: john
    last name: doe
    age: 45
    address: 888 1st street

    first name: joe
    last name: brown
    age: 32
    address: 777 2nd street

    ........

    more entries here

    ......


    How do I convert this to excel format

    first name last name age address
    john doe 45 888 1st street
    joe brown 32 777 2nd street

    ......


    I have come up with something but no where close to what i want it to do

    ======================================================

    Option Explicit

    Dim objUser, strExcelPath, objExcel, objSheet, objFSO, objFile, strline



    Const ForReading = 1

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\scripts\hosts.txt", ForReading)
    strExcelPath = "c:\scripts\sg3.xls"



    ' Bind to Excel object.
    On Error Resume Next
    Set objExcel = CreateObject("Excel.Application")
    If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Excel application not found."
    Wscript.Quit
    End If
    On Error GoTo 0


    objExcel.Workbooks.Add

    Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
    objSheet.Name = "test"


    Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine
    ' Wscript.Echo strLine
    objSheet.Cells(1, 1).Value = strline
    objSheet.Cells(1, 2).Value = strline
    objSheet.Cells(1, 3).Value = strline
    objSheet.Cells(1, 4).Value = strline

    Loop


    objExcel.ActiveWorkbook.SaveAs strExcelPath
    objExcel.ActiveWorkbook.Close
    objExcel.Application.Quit

    =========================================================

    The above only reads the text file (which currently has one column) and put
    them in a row format in excel

    any help appreciated

  6. #6
    Monitor Guest

    Re: script to extract data from text file and put into excel format

    In your first post you asked "Is there a script that will extract data from
    text file and put it into
    Excel column/rows format?". It implied that you know something about
    scripting in general abut not enough about the spreadsheet aspect. Pegasus
    gave you a direct answer to this question, and he also gave you a link with
    some suitable examples.

    In your follow-up question you appear to have ignored that link, asking for
    further guidance again, which Pegasus gave you. Once more you asked for
    additional details.

    It's probably time for you to be open and honest. Insead of asking "Is there
    a script that will extract data from text file and put it into Excel
    column/rows format?", you might ask "I know nothing at all about scripting,
    I have no intention of learning it but I'm looking for someone to do my work
    for me." It would make things a lot clearer.

  7. #7
    tony Guest
    I am trying to make it work on my own but getting stuck. Could really use
    some help. No i dont want someone to do my homework but some help would be

    The usual method in such cases is to post the code and report
    where one gets stuck. This is likely to attract several responses.

  8. #8
    Tom Lavedas Guest

    Re: script to extract data from text file and put into excel format

    Here is a adaptation of the code you posted that assumes the data as
    delimited with a TAB character. Without that or another similar
    delimiter, like a commas, it would be very hard to parse the data
    lines accurately.

    So the data looks like this ...

    first name[tab]last name[tab]age[tab]address
    john[tab]doe[tab]45[tab]888 1st street
    joe[tab]brown[tab]32[tab]777 2nd street

    and the code like this ...

    Option Explicit

    Dim objUser, strExcelPath, objExcel, objSheet, _
    objFSO, objFile, aline, aLines, irow, icol

    Const ForReading = 1

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("testing.txt", _
    ForReading)
    strExcelPath = "c:\scripts\sg3.xls"

    ' Bind to Excel object.
    'On Error Resume Next
    Set objExcel = CreateObject("Excel.Application")
    If (Err.Number <> 0) Then
    On Error GoTo 0
    Wscript.Echo "Excel application not found."
    Wscript.Quit
    End If
    On Error GoTo 0

    objExcel.visible = true
    objExcel.Workbooks.Add

    Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)
    objSheet.Name = "test"

    aLines = split(objFile.ReadAll, vbnewline)
    For irow = 1 to Ubound(aLines) + 1
    aline = split(aLines(irow-1), vbTab)
    for icol = 1 to Ubound(aline) + 1
    objSheet.Cells(irow, icol).value = aline(icol-1)
    Next ' icol
    next ' irow

  9. #9
    McKirahan Guest

    Re: script to extract data from text file and put into excel format

    You omitted "Category".

    Which part(s) repeat?
    "Category: " through "Message: "
    and/or
    "User Name: " through Source Port: "

    That is, which is your text file more like:

    a) Multiple "User Name" per "Category" (or "Computer Name"):

    Category:
    Message:

    User Name:
    Source Port:

    User Name:
    Source Port:

    User Name:
    Source Port:

    Category:
    Message:

    User Name:
    Source Port:

    User Name:
    Source Port:

    User Name:
    Source Port:

    b) One "User Name" per "Category" (or "Computer Name"):

    Category:
    Message:

    User Name:
    Source Port:

    Category:
    Message:

    User Name:
    Source Port:

  10. #10
    McKirahan Guest
    In both data samples ": " (colon space) appears to be the delimiter.

    However, one line ends with a colon "Message: Successful Logon:".

    Shouldn't be a problem if that final ":" is not followed by a space. Even if
    it is, there are a number of ways in which one could consider only the first
    instance of the delimiter string.

  11. #11
    Al Dunbar Guest

    Re: script to extract data from text file and put into excel format

    Don't take Monitor's comments too personally. As I read your questions I
    thought it was quite clear that you were having the typical newbie's problem
    of not knowing how to properly ask the question.

  12. #12
    tdubb Guest

    Re: script to extract data from text file and put into excel format

    Its more like (b)

    actually like this

    ================================================================================


    Category: 2 string Logon/Logoff
    ComputerName: xxxxxxxxxxxx
    Logfile: Security source Security
    EventCode: 528
    EventType: 4
    Type: Audit Success
    User: domainname\username
    Message: Successful Logon:

    User Name: username

    Domain: domainname

    Logon ID: (0x0,0x245D6D8)

    Logon Type: 10

    Logon Process: User32

    Authentication Package: Negotiate

    Workstation Name: xxxxxxxxxxx

    Logon GUID: {6bf7409a-dc43-e893-6355-dcf937334df5}

    Caller User Name: xxxxxxxxxxx

    Caller Domain: domainname

    Caller Logon ID: (0x0,0x3E7)

    Caller Process ID: 4320

    Transited Services: -

    Source Network Address: x.x.x.x.

    Source Port: 46563

    Category: 2 string Logon/Logoff
    ComputerName: xxxxxxxxxx
    Logfile: Security source Security
    EventCode: 528
    EventType: 4
    Type: Audit Success
    User: domainname\username
    Message: Successful Logon:

    User Name: username

    Domain: domainname

    Logon ID: (0x0,0x244D6EC)

    Logon Type: 10

    Logon Process: User32

    Authentication Package: Negotiate

    Workstation Name: xxxxxxxxxxx

    Logon GUID: {bb6be495-9fff-24cb-3b48-c4bb0e78c245}

    Caller User Name: xxxxxxxxxx$

    Caller Domain: domainname

    Caller Logon ID: (0x0,0x3E7)

    Caller Process ID: 4044

    Transited Services: -

    Source Network Address: x.x.x.x

    Source Port: 46560

  13. #13
    tdubb Guest

    Re: script to extract data from text file and put into excel format

    Ok I am thinking maybe using this file instead, the delimiter is ":"

    dn: CN=John Doe,CN=Users,DC=domain,DC=com
    cn: John Doe
    title: IT Manager
    description: IT Manager
    postalCode: 11111
    physicalDeliveryOfficeName: 111 First St. Kentucky
    telephoneNumber: 1-111-111-1111
    givenName: John
    distinguishedName: CN=John Doe,CN=Users,DC=domain,DC=com
    instanceType: 4
    displayName: John Doe
    uSNCreated: 119935722
    badPasswordTime: 128493834104953026
    lastLogoff: 0
    lastLogon: 128502585843800706
    scriptPath: logon.vbs
    pwdLastSet: 128444632154282347
    primaryGroupID: 513
    objectSid:: AQUAAAAAAAUVAAAANVeTEvpHVwPperlbvpoAAA==
    accountExpires: 128577311990000000
    logonCount: 253
    sAMAccountName: johndoe
    sAMAccountType: 805306368


    the fields I need as colums are the dn, objectClass etc.... in excel.

  14. #14
    tdubb Guest

    Re: script to extract data from text file and put into excel format

    what if the format was like this and the delimited is a ":"

    first name: john
    last name: doe
    age: 45
    address: 888 1st street

    first name: joe
    last name: brown
    age: 32
    address: 777 2nd street

    How do I change the code to put this into excel?

  15. #15
    McKirahan Guest

    Re: script to extract data from text file and put into excel format

    Will this help?

    It will create a CSV file which, when double-clicked, will open up
    in MS-Excel. It can be adapted to write diectly to MS-Excel.

    Option Explicit
    '****
    '* Read "cTXT" and write "cCSV".
    '****
    '*
    '* Declare Constants
    '*
    Const cVBS = "tdubb123.vbs"
    Const cTXT = "tdubb123.txt"
    Const cCSV = "tdubb123.csv"
    '*
    '* Declare Globals
    '*
    Dim sDIR
    sDIR = WScript.ScriptFullName
    sDIR = Left(sDIR,InStrRev(sDIR,"\"))
    '*
    '* Declare Variables
    '*
    Dim str1ST
    str1ST = ""
    Dim arrCTF()
    Dim intCTF
    intCTF = 0
    Dim strCTF
    Dim arrDIC()
    Dim intDIC
    intDIC = 0
    Dim strDIC
    Dim arrOTF
    Dim intOTF
    Dim strOTF
    Dim intPOS
    Dim strTXT
    Dim arrVAL()
    Dim intVAL
    Dim strVAL
    '*
    '* Declare Objects
    '*
    Dim objCTF
    Dim objDIC
    Set objDIC = CreateObject("Scripting.Dictionary")
    Dim objFSO
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objOTF
    '*
    '* Read file
    '*
    Set objOTF = objFSO.OpenTextFile(sDIR & cTXT,1)
    strOTF = objOTF.ReadAll
    Set objOTF = Nothing
    '*
    '* Parse file
    '*
    arrOTF = Split(strOTF,vbCrLf)
    For intOTF = 0 To UBound(arrOTF)
    strTXT = arrOTF(intOTF)
    If InStr(strTXT,vbTab) > 0 Then strTXT = Replace(strTXT,vbTab,"
    ")
    If InStr(strTXT,Chr(34)) > 0 Then
    MsgBox "Data contains quotation marks!",vbCritical,cVBS
    Exit For
    End If
    intPOS = InStr(strTXT,": ")
    If intPOS > 0 Then
    '*
    '* Split each valid line into name/value pair
    '*
    strDIC = Trim(Left(strTXT,intPOS-1))
    strVAL = Trim(Mid(strTXT,intPOS+2))
    '*
    '* Build array with data
    '*
    If str1ST = strDIC Then
    intCTF = intCTF + 1
    ReDim Preserve arrCTF(intCTF)
    arrCTF(intCTF) = Join(arrVAL,",")
    Erase arrVAL
    ReDim Preserve arrVAL(UBound(arrDIC))
    End If
    '*
    '* Identify first column name
    '*
    If str1ST = "" Then str1ST = strDIC
    '*
    '* Build dictionary and array of column names
    '*
    If Not objDIC.Exists(strDIC) Then
    objDIC.Add strDIC, intDIC
    ReDim Preserve arrVAL(intDIC)
    ReDim Preserve arrDIC(intDIC)
    arrDIC(intDIC) = strDIC
    intDIC = intDIC + 1
    End If
    '*
    '* Build array of column values
    '*
    intVAL = CInt(objDIC.Item(strDIC))
    If InStr(strVAL,",") Then strVAL = Chr(34) & strVAL & Chr(34)
    arrVAL(intVAL) = strVAL
    End If
    Next
    '*
    '* Build array with data and columns
    '*
    If str1ST <> "" Then
    intCTF = intCTF + 1
    ReDim Preserve arrCTF(intCTF)
    arrCTF(intCTF) = Join(arrVAL,",")
    arrCTF(0) = Join(arrDIC,",")
    End If
    '*
    '* Write file
    '*
    Set objCTF = objFSO.CreateTextFile(sDIR & cCSV)
    For intCTF = 0 To UBound(arrCTF)
    strCTF = arrCTF(intCTF)
    objCTF.WriteLine(strCTF)
    Next
    Set objCTF = Nothing
    '*
    '* Destroy Objects
    '*
    Set objDIC = Nothing
    Set objFSO = Nothing
    '*
    '* Finish Message
    '*
    MsgBox intCTF & " rows.",vbInformation,cVBS

Page 1 of 2 12 LastLast

Similar Threads

  1. How to save text field with zero in .csv format from Excel
    By Eeshika18 in forum MS Office Support
    Replies: 2
    Last Post: 10-02-2012, 05:27 PM
  2. How to format multiple text boxes in excel
    By Raju Chacha in forum Windows Software
    Replies: 1
    Last Post: 07-01-2012, 04:54 PM
  3. Can i extract text from jpg file
    By Ransom in forum Windows Software
    Replies: 5
    Last Post: 05-03-2010, 03:49 AM
  4. How can I convert text data into table format?
    By Ram Bharose in forum Windows Software
    Replies: 7
    Last Post: 30-12-2009, 02:58 PM
  5. Extract data from AD and save in TXT file.
    By Tom Ja in forum Windows Server Help
    Replies: 4
    Last Post: 30-07-2007, 09:39 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,411,531.82318 seconds with 17 queries