0

READ a text file line by line and store it into string if a particular pattern is found ,then output the pattern into differnt string ?

5
Contributors
7
Replies
36
Views
4 Years
Discussion Span
Last Post by ponnu
0

Dim file_id As Integer
Dim strline As String
Dim array_item() As String

'Open file
file_id = FreeFile

Open "D:\input.txt" For Input As #file_id

Dim i As Integer
i = 0

Do Until EOF(file_id)

Line Input #file_id, strline

ReDim Preserve array_item(i)

array_item(i) = strline

i = i + 1
Loop

Close #file_id
End Sub

Edited by ponnu

1

so, What wrong with your code? any errors?
What you mean about "particular pattern"?
Post your text file sample.

0

to find the pattern just loop through your array with a for loop and compare the string with your pattern.

Edited by tinstaafl

1

Try putting this in your loop:

If InStr(strline, "pattern", vbTextCompare) Then
    <do whatever you want with it here>
end if

I think that will do away with the array then.

1

Dim FileNum As Integer
Dim DataLine As String

FileNum = FreeFile()
ctr = 13
Open "Directory\of\file.text" For Input As #FileNum 'Opening a text file using macro

While Not EOF(FileNum) 'check if end of file
Line Input #FileNum, DataLine ' read in data 1 line at a time

Wend

try this..
Dont forget to mark this solve your problem is solve.

happy coding

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.