1 Attachment(s)
extract data from text file
i want extract data from "sample.txt" and save the data which have been extract to a new text file.
when have "START 129, S",then get the data below it & before the "stop"
for example:
START 129, S, 125=1;156=1;133=300000
100.05062000 3.69064000;49
STOP
START 129, S, 125=1;156=1;133=250000;93=1
100.22346000 2.88787000;43
100.18942000 2.91320000;39
STOP
data after extract is like below:
100.05062000 3.69064000;49
100.22346000 2.88787000;43
100.18942000 2.91320000;39
how to write the code?
Re: extract data from text file
It's simple logic... Show what you have so far.
Re: extract data from text file
Quote:
Originally Posted by randem
It's simple logic... Show what you have so far.
Code:
Private Sub Command1_Click()
Dim intff As Integer
Dim strAll() As String
intff = FreeFile
Open "D:\Testing\ENC Project\Setting\Sample.txt" For Input As #intff
strAll = Split(Input(LOF(intff), #intff) & vbCrLf, "START")
Close #intff
end sub
so far, i just doing the code above. i don't know how to write code when meet "START 129, S", then get the data from next line and stop until meet "STOP".After that, save the data which have been extract to a new text file.
Re: extract data from text file
'How about something simple like this:
vb Code:
Dim intff As Integer
Dim intFOut as Integer
Dim strLine As String
intff = FreeFile()
Open "D:\Testing\ENC Project\Setting\Sample.txt" For Input As #intff
intFOut = FreeFile()
Open "D:\Testing\ENC Project\Setting\SampleExtract.txt" For Output as #intFOut
Do Until EOF(intff)
Line Input #intff,strLine
If Not (strLine Like "START*" or strLine Like "STOP*") Then
Print #intFOut, strLine
End If
Loop
Close #intff
Close #intFOut
Re: extract data from text file
Also look at the Instr Function to assist. The split function is not goint to help you here unless you just use it to fill an array with the lines of the file.
Re: extract data from text file
Quote:
Originally Posted by codemasterj
'How about something simple like this:
vb Code:
Dim intff As Integer
Dim intFOut as Integer
Dim strLine As String
intff = FreeFile()
Open "D:\Testing\ENC Project\Setting\Sample.txt" For Input As #intff
intFOut = FreeFile()
Open "D:\Testing\ENC Project\Setting\SampleExtract.txt" For Output as #intFOut
Do Until EOF(intff)
Line Input #intff,strLine
If Not (strLine Like "START*" or strLine Like "STOP*") Then
Print #intFOut, strLine
End If
Loop
Close #intff
Close #intFOut
the code above just take out the "start" line and "stop" line.but, what i want is only need the value between the " START 129, S,...."line and "stop" line.
the result i want to get from "sample.txt" and save as another file is like below.
100.72022000 4.01296000;-1.2
100.67719000 4.01361000;1.5
100.65643000 4.01546000;7
100.63092000 4.01437000;8.5
100.63654000 3.99852000;9.8
100.05062000 3.69064000;49
100.22346000 2.88787000;43
100.18942000 2.91320000;39
100.23730000 2.92678000;25
101.05651000 3.39117000;5.6
99.71389000 3.85145000;35
99.81780000 3.88552000;42
Re: extract data from text file
Based on your example, I'm highlighting data you're extracting in red:
Header
2.85667000
99.70000000
4.01666940
101.46667000
2
Decimal
1.01.33
Some description
EndHeader
START 129, S, 125=1;156=1;133=300000
100.05062000 3.69064000;49
STOP
START 129, S, 125=1;156=1;133=250000;93=1
100.22346000 2.88787000;43
100.18942000 2.91320000;39
STOP
You're question is unclear. Are you referring to that "header" information in the file? You just want to start outputting what's after that between start and stop?
How about this:
vb Code:
Dim intff As Integer
Dim intFOut as Integer
Dim strLine As String
Dim blnOutputOn As Boolean
intff = FreeFile()
Open "D:\Testing\ENC Project\Setting\Sample.txt" For Input As #intff
intFOut = FreeFile()
Open "D:\Testing\ENC Project\Setting\SampleExtract.txt" For Output as #intFOut
Do Until EOF(intff)
Line Input #intff,strLine
If strLine Like "START*" And Not EOF(#intff) Then
'ignore the start line, read the next line.
blnOutputOn = True
Line Input #intff,strLine
ElseIf strLine Like "STOP*" Then
'Stop outputting data
blnOutputOn = False
End If
If blnOutputOn Then
Print #intFOut, strLine
End If
End If
Loop
Close #intff
Close #intFOut
Re: extract data from text file
I think you need to loop when you get to a START and exit it when you get to a STOP. Also this code caters for the specific text you specified at the begining of the thread. You can change it to the name of a variable and pass values of your choice into.
Code:
Dim iInput As Integer
Dim iOutput As Integer
Dim sLine As String
iInput = FreeFile
Open "c:\sample.txt" For Input As #iInput
iOutput = FreeFile
Open "c:\sampleout.txt" For Output As #iOutput
Do Until EOF(iInput)
Line Input #iInput, sLine
If InStr(sLine, "START 129, S") Then
Do While Not EOF(iInput)
Line Input #iInput, sLine
If InStr(sLine, "STOP") Then
Exit Do
Else
Print #iOutput, sLine
End If
Loop
End If
Loop
Close #iInput
Close #iOutput
:D