Parsing a text file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way to parse a text file?

I have a extructured text file from a dump system and I want to be able to
look up, line by line for some specific strings like 'Username' and 'number',
like:

###### Identification ######
Line 1, doesn't matter what exist here
Line 2, doesn't matter what exist here
Username: u_myuser1
Line 4,doesn't matter what exist here
Number: 1234
Line 6,doesn't matter what exist here
Line 7,doesn't matter what exist here
Username: u_myuser2
Line 8,doesn't matter what exist here
Number: 1890
### end ###

.... and extract the user and the number for each user.
Any ideias?
thanks
 
You can use somting like:



Sub ReadTextFile

Dim strFile As String
Dim intF As Integer
Dim strLineBuf As String

dim rstData as dao.RecordSet


set rstData = currentdb.Openrecordset("tblData")

strFile = "c:\my data\MyData.txt"

intF = FreeFile()
Open strFile For Input As #intF

Do While EOF(intF) = False
rstData.AddNew

Line Input #intF, strLineBuf
Line Input #intF, strLineBuf
Line Input #intF, strLineBuf
rstdata!Username1 = split(strLinebuf,"Username: ")(1)
Line Input #intF, strLineBuf
Line Input #intF, strLineBuf
rstData!Num1 = split(strLinebuf,"number: ")(1)
Line Input #intF, strLineBuf
Line Input #intF, strLineBuf
Line Input #intF, strLineBuf
rstData!Username2 = split(strLinebuf,"Username: ")(1)
Line Input #intF, strLineBuf
Line Input #intF, strLineBuf
rstData!Num2 = split(strLinebuf,"number: ")(1)

rstData.AddNew
Loop
Close intF

End If

End Function


The above is not complete...but it is air code of the basic loop you need.
It is not clear if you have "many" records in a single text file, or simply
one record per text file. And, also, perhaps the number2, and user name 2
should start a new record if this is really repeating data (so, just include
an additional "addnew" + "update" for the reocrdset....

You *WILL* need basic coding skills to parse out that data....
 
Nuno said:
Is there a way to parse a text file?

I have a extructured text file from a dump system and I want to be able to
look up, line by line for some specific strings like 'Username' and 'number',
like:

###### Identification ######
Line 1, doesn't matter what exist here
Line 2, doesn't matter what exist here
Username: u_myuser1
Line 4,doesn't matter what exist here
Number: 1234
Line 6,doesn't matter what exist here
Line 7,doesn't matter what exist here
Username: u_myuser2
Line 8,doesn't matter what exist here
Number: 1890
### end ###

... and extract the user and the number for each user.


Use the standard VB I/O statements Open and Input Line
along with the usual string functions Left, Mid, etc.

Check VBA Help for details.
 
Back
Top