Results 1 to 8 of 8

Thread: extract data from text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    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?
    Attached Files Attached Files

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: extract data from text file

    It's simple logic... Show what you have so far.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    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.

  4. #4
    New Member
    Join Date
    Apr 2007
    Posts
    14

    Re: extract data from text file

    'How about something simple like this:
    vb Code:
    1. Dim intff As Integer
    2. Dim intFOut as Integer
    3. Dim strLine As String
    4.  
    5. intff = FreeFile()
    6. Open "D:\Testing\ENC Project\Setting\Sample.txt" For Input As #intff
    7. intFOut = FreeFile()
    8. Open "D:\Testing\ENC Project\Setting\SampleExtract.txt" For Output as #intFOut
    9.  
    10. Do Until EOF(intff)
    11.   Line Input #intff,strLine
    12.   If Not (strLine Like "START*" or strLine Like "STOP*") Then
    13.     Print #intFOut, strLine
    14.   End If
    15. Loop
    16.  
    17. Close #intff
    18. Close #intFOut
    Last edited by codemasterj; Apr 2nd, 2007 at 12:35 PM.

  5. #5
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2006
    Posts
    120

    Re: extract data from text file

    Quote Originally Posted by codemasterj
    'How about something simple like this:
    vb Code:
    1. Dim intff As Integer
    2. Dim intFOut as Integer
    3. Dim strLine As String
    4.  
    5. intff = FreeFile()
    6. Open "D:\Testing\ENC Project\Setting\Sample.txt" For Input As #intff
    7. intFOut = FreeFile()
    8. Open "D:\Testing\ENC Project\Setting\SampleExtract.txt" For Output as #intFOut
    9.  
    10. Do Until EOF(intff)
    11.   Line Input #intff,strLine
    12.   If Not (strLine Like "START*" or strLine Like "STOP*") Then
    13.     Print #intFOut, strLine
    14.   End If
    15. Loop
    16.  
    17. Close #intff
    18. 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

  7. #7
    New Member
    Join Date
    Apr 2007
    Posts
    14

    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:
    1. Dim intff As Integer
    2. Dim intFOut as Integer
    3. Dim strLine As String
    4. Dim blnOutputOn As Boolean
    5.  
    6. intff = FreeFile()
    7. Open "D:\Testing\ENC Project\Setting\Sample.txt" For Input As #intff
    8. intFOut = FreeFile()
    9. Open "D:\Testing\ENC Project\Setting\SampleExtract.txt" For Output as #intFOut
    10. Do Until EOF(intff)
    11.   Line Input #intff,strLine
    12.   If strLine Like "START*" And Not EOF(#intff) Then
    13.     'ignore the start line, read the next line.  
    14.     blnOutputOn = True
    15.     Line Input #intff,strLine
    16.   ElseIf strLine Like "STOP*" Then
    17.     'Stop outputting data
    18.     blnOutputOn = False
    19.   End If
    20.   If blnOutputOn Then
    21.     Print #intFOut, strLine
    22.   End If
    23. End If
    24. Loop
    25. Close #intff
    26. Close #intFOut

  8. #8
    Member
    Join Date
    Mar 2007
    Posts
    46

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width