-
From a beginner.
I am presented with a .txt file every month that contains a log of air-ground data communications between air traffic control and aircraft. The file is usually in the order of 5000 - 6000 pages. I am writing an application to make analysis easier. One task is to find where the ground system cancels the data link with a specific aircraft, which is characterised by the text line - CANCEL ALL CONTRACTS. I am able to extract each occurrence of this line and write it to a different file, however, the information that I really need (aircraft callsign and time stamp) occurs two lines earlier. How do I extract and add the previous 2 lines so that I write all three lines to the new file? I haven't tried an array as yet.
I use two Common Dialog Controls, one to select the file to read, the other to select the file to write and then the respective files are displayed in lblDataFile and lblFileWrite. I also use a text box (txtInput) to input the string that I am searching for.
Example of work so far:
Private Sub cmdSearch_Click()
Dim s1 As String
Dim s2 As String
Dim s3 As String
s1 = (lblDataFile.Caption)
s2 = (txtInput.Text)
s3 = (lblFileWrite.Caption)
'Open the file that data will be read from.
Open s1 For Input As #1
'Open or create write file.
Open s3 For Append As #2
Do While Not EOF(1)
Line Input #1, s1
If s1 = s2 Then Write #2, (s1)
Loop
'Close read file.
Close #1
'Close write file before reopening in another mode.
Close #2
'Displays message box when all required data is written to selected file.
MsgBox "Program has finished writing to file."
End Sub
-
You could try this...
Code:
Private Sub cmdSearch_Click()
Dim s1 As String
Dim s2 As String
Dim s3 As String
Dim PreviousLine1 As String
Dim PreviousLine2 As String
s1 = (lblDataFile.Caption)
s2 = (txtInput.Text)
s3 = (lblFileWrite.Caption)
'Open the file that data will be read from.
Open s1 For Input As #1
'Open or create write file.
Open s3 For Append As #2
Do While Not EOF(1)
Line Input #1, s1
'found the line
If Instr(s1,"CANCEL ALL CONTRACTS") <> 0 Then
' do whatever you want here
' and the variables PreviousLine1 and PreviousLine2
' ought to contain the two previous lines....
Else
PreviousLine2 = PreviousLine1
PreviousLine1 = s1
End If
If s1 = s2 Then Write #2, (s1)
Loop
'Close read file.
Close #1
'Close write file before reopening in another mode.
Close #2
'Displays message box when all required data is written to selected file.
MsgBox "Program has finished writing to file."
End Sub
DocZaf
{;->
-
See if this helps. Add two variables:
Code:
Dim strRec As String
Dim blnFirst As Boolean
Modify your read loop as follows:
Code:
blnFirst = True
Do Until EOF(1)
Line Input #1, strRec
If strRec = "CANCEL ALL CONTRACTS" Then
Write #2, s1
Write #2, s2
Else
If blnFirst Then
s1 = strRec
blnFirst = False
Else
s2 = strRec
blnFirst = True
End If
End If
Loop
[Edited by BruceG on 08-15-2000 at 09:12 AM]