1 Attachment(s)
Parsing Experts!!! Need help.
Hello guys! Im having trouble parsing this file although it's a bit easy i can't get it neat and fast. Any suggestion is well appreciated.
Here is my sample text file.
Quote:
Name <1:WALKER, JOHNNY>
File Number <2:123456>
Record Number <3:111111>
Filename: WALKER, JOHNNY_123456_111111 script.txt
<End>
CASE: 0001
NAME: JOHNNY WALKER
RECORD NUMBER: 111111
Description is here. The quick brown fox jumps over the lazy dog. Blah blah blah.
CASE: 0002
NAME: JACKIE CHAN
RECORD NUMBER: 222222
Pneumonoultramicroscopicsilicovolcanoconiosis.
CASE: 0003
NAME: JET LI
RECORD NUMBER: 333333
Lol lol lol lol lol lol lol.
CASE: 0004
NAME: WONG FEI HONG
RECORD NUMBER: 444444
Any random description here.
I need to separate this file into 4 textfiles however the header must be changed according to the name, record number and description. Don't bother the case number and the Filenumber is the same for all of them.
Case 001 have already the correct header. So it will be saved as "WALKER, JOHNNY_123456_111111 script.txt"
Quote:
Name <1:WALKER, JOHNNY>
File Number <2:123456>
Record Number <3:111111>
Filename: WALKER, JOHNNY_123456_111111 script.txt
<End>
CASE: 0001
NAME: JOHNNY WALKER
RECORD NUMBER: 111111
Description is here. The quick brown fox jumps over the lazy dog. Blah blah blah.
Case 002 shoud be saved as "CHAN, JACKIE_123456_222222 script.txt" and the header should be replaced accordingly.
Quote:
Name <1:CHAN, JACKIE>
File Number <2:123456>
Record Number <3:222222>
Filename: CHAN, JACKIE_123456_222222 script.txt
<End>
CASE: 0002
NAME: JACKIE CHAN
RECORD NUMBER: 222222
Pneumonoultramicroscopicsilicovolcanoconiosis.
and so on and so forth. the cases could reached up to 30 cases that's why im looking for an elegant way of doing this or even faster.
So there's one file and the output would be 4 files in that case.
Thank you and God bless! :wave:
Re: Parsing Experts!!! Need help.
Not sure if this is what your after, but thought i'd give it a try
It will set up an array with the data to be written into the log. Use GetData to return the log contents you need.
Code:
Private Type CaseData
CaseNumber As Long
NAME As String
RecordNumber As Long
Description As String
End Type
Dim Cases() As CaseData
Private Sub Command1_Click()
Dim Prts() As String
Dim x As Integer
Dim Num As Integer
Dim Pos As Integer
Prts() = Split(Text1.Text, vbCrLf)
For x = 0 To UBound(Prts())
If InStr(1, Prts(x), "CASE:", vbTextCompare) And Pos = 0 Then
ReDim Preserve Cases(Num)
Cases(Num).CaseNumber = Val(Mid(Prts(x), 6))
Pos = 1
End If
If InStr(1, Prts(x), "NAME:", vbTextCompare) And Pos = 1 Then
Cases(Num).NAME = Mid(Prts(x), 6)
Pos = 2
End If
If Pos = 3 Then
Cases(Num).Description = Prts(x)
Num = Num + 1
Pos = 0
End If
If InStr(1, Prts(x), "RECORD NUMBER:", vbTextCompare) And Pos = 2 Then
Cases(Num).RecordNumber = Val(Mid(Prts(x), 15))
Pos = 3
End If
Next
End Sub
Private Function GetData(Index As Integer) As String
Dim Nms As String
Nms = Split(Trim(Cases(Index).NAME), " ")
GetData = "Name " & vbTab & "<1:" & Nms(1) & ", " & Nms(0) & ">" & vbCrLf & _
"File Number " & vbTab & "<2:123456>" & vbCrLf & _
"Record Number " & vbTab & "<3:" & Cases(Index).RecordNumber & ">" & vbCrLf & _
"Filename: " & Nms(1) & ", " & Nms(0) & "_123456_" & Cases(Index).RecordNumber & " script.txt" & vbCrLf & _
"<End>" & vbCrLf & vbCrLf & _
"CASE: " & Format(Cases(Index).CaseNumber, "0000") & vbCrLf & _
"NAME: " & Cases(Index).NAME & vbCrLf & _
"RECORD NUMBER: " & Cases(Index).RecordNumber & vbCrLf & _
Cases(Index).Description
End Function
Re: Parsing Experts!!! Need help.
Thanks for your reply however I need to open the file then create another 4 textfiles in that case. something like this.
vb Code:
'path
myPath = "C:\test\sample.txt"
ff = FreeFile
Open myPath For Input As ff
myArr() = Split(Input(LOF(ff), ff), vbCrLf & vbCrLf)
Close ff
then
vb Code:
Open "theCorrectFilenameHere" For Output As ff
Thanks again for the help!
Hey Andrew I was wondering where is the log written. :confused:
Re: Parsing Experts!!! Need help.
Added the part to write the files. The changed code (from my above post) is in bold.
Code:
Private Type CaseData
CaseNumber As Long
NAME As String
RecordNumber As Long
Description As String
End Type
Dim Cases() As CaseData
Private Sub Command1_Click()
Dim Prts() As String
Dim x As Integer
Dim Num As Integer
Dim Pos As Integer
Prts() = Split(Text1.Text, vbCrLf)
For x = 0 To UBound(Prts())
If InStr(1, Prts(x), "CASE:", vbTextCompare) And Pos = 0 Then
ReDim Preserve Cases(Num)
Cases(Num).CaseNumber = Val(Mid(Prts(x), 6))
Pos = 1
End If
If InStr(1, Prts(x), "NAME:", vbTextCompare) And Pos = 1 Then
Cases(Num).NAME = Mid(Prts(x), 6)
Pos = 2
End If
If Pos = 3 Then
Cases(Num).Description = Prts(x)
Num = Num + 1
Pos = 0
End If
If InStr(1, Prts(x), "RECORD NUMBER:", vbTextCompare) And Pos = 2 Then
Cases(Num).RecordNumber = Val(Mid(Prts(x), 15))
Pos = 3
End If
Next
End Sub
Private Function GetData(Index As Integer) As String
Dim Nms As String
Nms = Split(Trim(Cases(Index).NAME), " ")
GetData = "Name " & vbTab & "<1:" & Nms(1) & ", " & Nms(0) & ">" & vbCrLf & _
"File Number " & vbTab & "<2:123456>" & vbCrLf & _
"Record Number " & vbTab & "<3:" & Cases(Index).RecordNumber & ">" & vbCrLf & _
"Filename: " & GetFileName & vbCrLf & _
"<End>" & vbCrLf & vbCrLf & _
"CASE: " & Format(Cases(Index).CaseNumber, "0000") & vbCrLf & _
"NAME: " & Cases(Index).NAME & vbCrLf & _
"RECORD NUMBER: " & Cases(Index).RecordNumber & vbCrLf & _
Cases(Index).Description
End Function
Private Function GetFileName(Index As Integer) As String
Dim Nms As String
Nms = Split(Trim(Cases(Index).NAME), " ")
GetFileName = Nms(1) & ", " & Nms(0) & "_123456_" & Cases(Index).RecordNumber & " script.txt"
End Function
Sub SaveFiles()
Dim x As Integer
For x = 0 To UBound(Cases)
Open GetFileName(x) For Output As #1
Print #1, Cases(x)
Close #1
Next
End Sub
Quote:
Originally Posted by zynder
Hey Andrew I was wondering where is the log written. :confused:
There wasn't :blush: But its added now :D
Re: Parsing Experts!!! Need help.
OK thanks. I'll try it out later. Cheers mate!
Re: Parsing Experts!!! Need help.
Split(stream, vbCrLf & "CASE:") and you will get a string array eg strCases() with index 0 containing original header info.
Redim another string array eg strHeader(Ubound(strCases)) for individual header of each CASE file.
Populate strHeader() array {skip index zero and strHeader(1) is just the same as original header strCases(0)} in a way that strHeader(2) is the header of strCases(2), etc. You will get basic info from strCases(0) then add the per case file header info for the other strCases() array indices (headers for index 2 and above are the ones you need to create).
So when you print the files, you print first from strHeader(index) then print strCases(index), don't forget to reinsert "CASE:", and that's in an iteration to Ubound() or index variable increments.
Re: Parsing Experts!!! Need help.
Thank you, leinad31!
Once again, I learned that I cannot trust Microsoft Documentation.:o
MSDN says (Split(expression[, delimiter[, limit[, compare]]])
Quote:
delimiter Optional. String character used to identify substring limits. If omitted, the space character (" ") is assumed to be the delimiter. If delimiter is a zero-length string, a single-element array containing the entire expression string is returned.
I assumed that since the delimiter description says that delimiter is a string character that it would have to be a SINGLE character.
This nugget will save me a bit of programming the the future! (Rate+=1)
Re: Parsing Experts!!! Need help.
No prob :)
The prepended vbCrLf ensures your only considering "CASE:" which is at the start of the line or not nested within the line, helps avoid per line read then test (eg. checking each line if it starts with "CASE:"), and gives you less array elements to process all of which simplifies things.