+ Reply to Thread
Results 1 to 12 of 12

Extract some lines from a .cfg/.txt file and paste it into an excel

  1. #1
    Registered User
    Join Date
    02-22-2022
    Location
    Mexico
    MS-Off Ver
    Office 2021
    Posts
    8

    Question Extract some lines from a .cfg/.txt file and paste it into an excel

    Hey guys,

    I got a hard one and I dont know how to start this.
    I have 150+ folders with 2-3 .cfg/.txt files (CS:GO config files) and I want to extract specific lines from them.
    So I need a script to search the 2-3 files for lines like:
    cl_crosshairalpha
    cl_crosshaircolor
    cl_bob_lower_amt
    cl_bobamt_lat

    In the files you can find the value for this lines and I need the whole line incl. value in an excel. It can happen that an file does not have on of the obove lines. In this case I dont wont an output.
    This would be the output of the attached sample file:
    cl_crosshairalpha "255"
    cl_crosshaircolor "4"
    cl_bob_lower_amt "21"
    cl_bobamt_lat "0.33"

    Is there a way to do this task or do I have to do it manually?
    Note: I had to convert the file into a .txt file because i cant upload a .cfg file!

    Thanks
    stinkzor
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Extract some lines from a .cfg/.txt file and paste it into an excel


    Hey,

    attach at least another text file and the expected workbook result according to both samples text files
    and an accurate explanation in order to process the folders or I could only post a demonstration to read a single text file …

  3. #3
    Registered User
    Join Date
    02-22-2022
    Location
    Mexico
    MS-Off Ver
    Office 2021
    Posts
    8

    Re: Extract some lines from a .cfg/.txt file and paste it into an excel

    Hey Marc,

    you are right, my explanation was not accurate enough.
    Please find attached the config.txt (please keep in mind that its originally a .cfg file but I cant upload a .cfg file) and a sample.xlsx.

    I think the biggest problem with scraping all data from all files/folders at once will fail because the file is always called config.cfg. The folder has a specific name...

    So I think the best is to do it file/folder per file/folder. What do you think?

    In the sample.xlsx from row 2-15 you can find the line I want it to search for, Ideally in 3 categories.

    As result I need each category in a separate cell, separating it with a semicolon followed by a space ("; ").

    You would safe me so much time if you could help me out.
    Thanks a lot.
    stinkzor
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,220

    Re: Extract some lines from a .cfg/.txt file and paste it into an excel

    I prepared the following code before you published the last post. The layout of the output is a bit different than what you wanted. But try it.
    Please Login or Register  to view this content.
    Artik

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Extract some lines from a .cfg/.txt file and paste it into an excel

    Select main folder when asked.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Extract some lines from a .cfg/.txt file and paste it into an excel

    Quote Originally Posted by stinkzor View Post
    As result I need each category in a separate cell, separating it with a semicolon followed by a space ("; ").
    Is this your final target ? A bit weird according to Excel purpose …

    And you forgot to describe your folder structure, any root one ?

  7. #7
    Registered User
    Join Date
    02-22-2022
    Location
    Mexico
    MS-Off Ver
    Office 2021
    Posts
    8

    Re: Extract some lines from a .cfg/.txt file and paste it into an excel

    wow, that works very well!! thank you!
    As mentioned I got 100+ folders with 2-3 files in it. The files in that folders have all the same name but the folders are unique.
    Is it possible to read all folders at once and sort the results, like in the sample attached?
    Player1
    result
    Player2
    result
    Player3
    result
    etc...
    Attached Files Attached Files

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Extract some lines from a .cfg/.txt file and paste it into an excel

    You must have same list like in sample1.xlsx.
    change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    02-22-2022
    Location
    Mexico
    MS-Off Ver
    Office 2021
    Posts
    8

    Re: Extract some lines from a .cfg/.txt file and paste it into an excel

    thank you for that quick reply!
    I just tried it and get an runtime error "52":
    filename or number wrong

    I cant figure out whats wrong... could you have a look?
    Attached Images Attached Images

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Extract some lines from a .cfg/.txt file and paste it into an excel

    Can you read fn when you get debug mode?

    You can see the value if you hover the cursor over variable "fn".

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this !


    According to your post #3 attachment with the post #7 expected layout a VBA demonstration for starters
    to paste to the top of the worksheet module then, as without any clear explanation neither any answer to my question,
    just amend in Demo1 procedure the root path within the codeline ImportFiles ThisWorkbook.Path & "\"
    or just save the workbook in the root folder :

    PHP Code: 
    Dim F%, K(), R&, T$()

    Sub ImportFiles(ByVal P$)
          Const 
    "config.cfg"
            
    Dim B%, S$(), C%, VD$
        If 
    Dir(N) > "" Then
                B 
    2
                Open P 
    For Input As #F
                
    Split(Input(LOF(F), #F), vbLf)
                
    Close #F
            
    With Application
                
    For 1 To UBound(K)
                        
    Filter(.IfError(.Match(K(C), S0), False), FalseFalse)
                    If 
    UBound(V) > -1 Then
                        
    If B Then T(11) = PB0
                        T
    (RC) = Join(.Index(SV), "; ")
                    
    End If
                
    Next
            End With
        End 
    If
              
    Split("")
              
    Dir$(P16)
        While 
    ""
              
    If Not D Like ".*" Then If (GetAttr(D) And 16Then ReDim Preserve S(UBound(S) + 1): S(UBound(S)) = "\"
              D = Dir$
        Wend
            For Each V In S:  ImportFiles V:  Next
    End Sub

    Sub Demo1()
      Const L = 19
        Dim C%
            Cells(L, 1).CurrentRegion.Clear
        With [A1].CurrentRegion.Columns
                ReDim K(1 To .Count), T(1 To Rows.Count + 1 - L, 1 To .Count)
            For C = 1 To .Count
              K(C) = Filter(Evaluate(Replace("
    TRANSPOSE(IF(#>0,#&"" *""))", "#", .Cells(3, C).Resize(.Rows.Count - 2).Address)), False, False)
            
    Next
        End With
            F 
    FreeFile
            R 
    0
            ImportFiles ThisWorkbook
    .Path "\"
            If R Then Cells(L, 1).Resize(R, UBound(T, 2)).Value2 = T
            Erase K, T
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 02-24-2022 at 04:14 PM. Reason: typo …

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: Extract some lines from a .cfg/.txt file and paste it into an excel

    I'm just about go offline,
    try replace "test" sub with
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Extract text between two words in a text file and paste to Excel
    By rasull13 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-06-2018, 03:38 PM
  2. [SOLVED] Extract lines of text from an e-mail chain and paste them in Excel
    By bnevena in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-27-2018, 12:29 PM
  3. Need to Read Text file and Paste required lines in different excel sheet based on Text
    By frazzlesole in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-05-2018, 08:34 PM
  4. [SOLVED] copy paste particular lines to other file
    By rickmeister in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-03-2013, 01:30 AM
  5. Convert Multiple Lines from Notepad extract to 1 cell in Excel
    By expertcashapp in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-27-2013, 01:51 AM
  6. Extract Data and paste in Summary File
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-04-2012, 08:54 PM
  7. Replies: 1
    Last Post: 10-17-2005, 04:05 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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1