-
Mar 30th, 2007, 03:24 AM
#1
Thread Starter
Member
txt file: getting data from the specified line number
Hi,
as using at simple database action, I searched the forum and the net, but I couldn't find the exact answer of the question: Can we get the data which we want from the txt files? for ex. I need the data on line 5.
have a nice day...
-
Mar 30th, 2007, 03:34 AM
#2
Frenzied Member
Re: txt file: getting data from the specified line number
One simple way would be to open the file and split it into an array using VbCrLf as the delimiter. Then, the line you need -1 (Because arrays are 0-based). So with this method, line 5, would be the 4th element in the array.
-
Mar 30th, 2007, 04:25 AM
#3
Junior Member
Re: txt file: getting data from the specified line number
This code will work too. It reads the file line by line and exits the loop after the 5th line which it shows in a MsgBox.
Code:
Dim fs
Dim ts
Dim line As String
Dim filepath As String
filepath = App.Path & "\test.txt"
Dim i As Long
i = 1
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(filepath, 1, False)
Do While Not ts.AtEndOfStream
line = ts.readline
If i = 5 Then
Exit Do
End If
i = i + 1
Loop
MsgBox line
Let me know if it helps
-
Mar 30th, 2007, 07:20 AM
#4
Member
Re: txt file: getting data from the specified line number
You can get all the text out and into an array; you can start the array at 1; you can omit blank lines....
Code:
Dim sLine As String
Dim iFileNo As Integer
Dim a()
Dim c As Integer
ReDim a(0)
c = 1
iFileNo = FreeFile
'open the file for reading
Open "C:\Test.txt" For Input As #iFileNo
'read the file until we reach the end
Do While Not EOF(iFileNo)
Input #iFileNo, sLine
'If sLine <> "" then <-- This will allow you to skip blank lines
ReDim Preserve a(c)
a(c) = sLine
c = c + 1
'endif
Loop
'close the file
Close #iFileNo
You can then get whatever line you want out of the array using the line number, so for line 5 use: a(5)
-
Apr 2nd, 2007, 01:48 AM
#5
Thread Starter
Member
Re: txt file: getting data from the specified line number
hi, sorry for I am late,
I was thinking use the txt file for my database app. which I previously used the excel that relativey slow.
In the weekend I tried on it and succeded. the lines consists of:
abc*dfg*fgh*dsa*....
I chosed "*" as the determining character of the elements of the array.
problem: when there is "," in the string statement, I can not take the strings after the coma. and the program gives error.
not: I will ask this problem as another quest!
thank you and have a nice day...
-
Apr 2nd, 2007, 01:50 AM
#6
Thread Starter
Member
Re: txt file: getting data from the specified line number
addition: the code part I used:
dim s() as string
....
s=split(state,"*",20)
.....
-
Apr 2nd, 2007, 02:03 AM
#7
Re: txt file: getting data from the specified line number
I guess your problem with "," is that you are only reading the first line of the file, and the code understands the "," as a lineending sign!
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Apr 2nd, 2007, 02:10 AM
#8
Thread Starter
Member
Re: txt file: getting data from the specified line number
Originally Posted by opus
I guess your problem with "," is that you are only reading the first line of the file, and the code understands the "," as a lineending sign!
hi, I wasn't know the "," sign is lineending mark !
in net, I meet "line input" and just I tried it and I guess It works.
apologise: I had to searched net first
excuse: I hadn't thought that I could find the answers in the net
have a nice day and excuse me...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|