Discussion:
parse a Text file
(too old to reply)
s***@gmail.com
2013-01-24 15:24:39 UTC
Permalink
Hi to all,
i've to parse a text file finding exactly some text in different lines.
For example:
file parse.txt
HATCH
5
A7122
330
309A6
100
AcDbEntity
8
QUOTE
62
1
100
AcDbHatch
10
0.0
20
0.0
30
HATCH
5
A7123
330
309A6
100
AcDbEntity
8
QUOTE
62
1 '<-I've to find this number
100
AcDbHatch
10
0.0
20
0.0
30
__________________
END OF FILE parse.txt

I would like to find the number after line with 62 (1) that is after this three lines
HATCH
5
A7123

I can read Lines from a txt file but i cant control the "next line parameter"
Start VBA CODE
Public Sub Parsedxf()

Dim sFileName As String
Dim iFileNum As Integer
Dim sBuf As String
Dim Fields As String
Dim TempStr As String
Dim strGenerator(0 To 3) As String
Dim i As Integer
i = 0

sFileName = "E:\Batch\parse.txt"
''//Does the file exist?
If Len(Dir$(sFileName)) = 0 Then
MsgBox ("Cannot find parse.txt")
End If

iFileNum = FreeFile()
Open sFileName For Input As iFileNum


Do While Not EOF(iFileNum)
Line Input #iFileNum, Fields
If Fields = "HATCH" Then

MsgBox (Fields)
End If
Loop
Close iFileNum
End Sub

End VBA CODE

The logic process that i would like to realize is:
Find "HASH"
if you find go to next line and find "5"
if you find go to next line and find "A7123" (if you dont find "A7123" then find "HASH" another time)
read the next line until reach "62"
go to the next line and manipulate "1" (for example replace 1 with a value in an excel cell)

Tips: the file that i've to parse have more than 6.000.000 rows so i have to find an efficient solution to manipulate le right row

Thanks in advance

Salmec
Auric__
2013-01-24 17:09:15 UTC
Permalink
Post by s***@gmail.com
i've to parse a text file finding exactly some text in different lines.
file parse.txt
[snip]
Post by s***@gmail.com
END OF FILE parse.txt
I would like to find the number after line with 62 (1) that is after this three lines
HATCH
5
A7123
I can read Lines from a txt file but i cant control the "next line parameter"
Start VBA CODE
Public Sub Parsedxf()
Dim sFileName As String
Dim iFileNum As Integer
Dim sBuf As String
Dim Fields As String
Dim TempStr As String
Dim strGenerator(0 To 3) As String
Dim i As Integer
i = 0
sFileName = "E:\Batch\parse.txt"
''//Does the file exist?
If Len(Dir$(sFileName)) = 0 Then
MsgBox ("Cannot find parse.txt")
End If
iFileNum = FreeFile()
Open sFileName For Input As iFileNum
Do While Not EOF(iFileNum)
Line Input #iFileNum, Fields
tester:
Select Case Trim$(Fields)
Case "HATCH"
Line Input #iFileNum, Fields
If Trim$(Fields) = "5" Then
Line Input #iFileNum, Fields
If Trim$(Fields) = "A7123" Then
fnd = True
Else
GoTo tester
End If
Else
GoTo tester
End If
Case "62"
If fnd Then
Line Input #iFileNum, Fields
Fields = Trim$(Fields)
'Fields now has the info you want;
'add your next step here.
'If you only need the first occurence, then:
Exit Do
End If
End Select
Post by s***@gmail.com
Loop
Close iFileNum
End Sub
End VBA CODE
Find "HASH"
if you find go to next line and find "5"
if you find go to next line and find "A7123" (if you dont find "A7123"
then find "HASH" another time) read the next line until reach "62"
go to the next line and manipulate "1" (for example replace 1 with a
value in an excel cell)
Future Excel-related questions would be better in this group:

microsoft.public.excel.programming
Post by s***@gmail.com
Tips: the file that i've to parse have more than 6.000.000 rows so i
have to find an efficient solution to manipulate le right row
See my changes to your code. Note that 3 lines of code (and 1 blank line)
are removed.
--
You are more than a trial and a problem.
s***@gmail.com
2013-01-25 08:33:26 UTC
Permalink
[...CUT]

THANKS A LOT!!
It works perfectly

king Regards

salmec

Loading...