Consulting

Results 1 to 5 of 5

Thread: How to Extract Title from a Text file and Paste to Excel

  1. #1

    How to Extract Title from a Text file and Paste to Excel

    I have a thousand text (*.TXT) files in my "C:\My Report" folder and I want to extract the TITLE values from each one of them and paste them to Excel. My problem is the way these text files are formatted. There are a lot of hard/carriage returns. See attached sample below:

    [IPC]
    B22C-5/00
    [TITLE]
    Casting waste sand regeneration roasting
    processor
    ???????????
    [ABSTRACT]

    Output in Excel file:
    Casting waste sand regeneration roasting processor

    Anyone with ideas?

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Which of these statements are true/False?

    All Titles are proceeded by the line "[TITLE]"
    All Titles are on two lines
    The line "[ABSTRACT]" is always preceeded by one line after the title.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Quote Originally Posted by SamT
    Which of these statements are true/False?

    All Titles are proceeded by the line "[TITLE]"
    All Titles are on two lines
    The line "[ABSTRACT]" is always preceeded by one line after the title.
    Hi Sam, thanks for the reply.

    All Titles are proceeded by the line "[TITLE]" >> TRUE
    All Titles are on two lines >> FALSE (it is variable)
    The line "[ABSTRACT]" is always preceeded by one line after the title >> TRUE

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    All titles are on one to max lines.

    What is max?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    [vba]
    sub M_snb()
    with createobject("scripting.filesystemobject")
    for each it in split(createobject("wscript.shell").exec("cmd /c Dir ""C:\my folder\*.txt"" /b").stdout.readall,vbcrlf)
    c00=c00 & vblf & replace(split(split(.opentextfile(it).readall,"[ABSTRACT]")(0),"[TITLE]")(1),vbcrlf," ")
    next
    end with

    sn=split(c00,"|")
    thisworkbook.sheets(1).cells(1).resize(ubound(sn)+1)=application.transpose( sn)
    End Sub
    [/vba]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •