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!
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 …
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
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...
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 N = "config.cfg" Dim B%, S$(), C%, V, D$ If Dir(P & N) > "" Then B = 2 Open P & N For Input As #F S = Split(Input(LOF(F), #F), vbLf) Close #F With Application For C = 1 To UBound(K) V = Filter(.IfError(.Match(K(C), S, 0), False), False, False) If UBound(V) > -1 Then If B Then T(R + 1, 1) = P: R = R + B: B = 0 T(R, C) = Join(.Index(S, V), "; ") End If Next End With End If S = Split("") D = Dir$(P, 16) While D > "" If Not D Like ".*" Then If (GetAttr(P & D) And 16) Then ReDim Preserve S(UBound(S) + 1): S(UBound(S)) = P & D & "\" 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 …
Bookmarks