parse a Text file

S

salmec

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
 
A

Auric__

salmec said:
i've to parse a text file finding exactly some text in different lines.
For example:
file parse.txt [snip]
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
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)

Future Excel-related questions would be better in this group:

microsoft.public.excel.programming
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top