How can i read only one line or specific line of a text file?
Printable View
How can i read only one line or specific line of a text file?
Public Sub ReadDetails(IsFilename As String)
' read the text from file
Set fso = CreateObject("Scripting.FileSystemObject")
Set tem = fso.OpenTextFile(IsFilename)
Do Until am = "end" Or am = "End" Or am = "END"
am = tem.readline
Loop
end Sub
This will read one line at a time, and in this spesific routine, it will stop when it hits the word "END" or "end" or "End". IsFilename = full path and filename.
hope this helps.
Replace the bold number with whatever line number you want to get.
Code:Private Sub Command1_Click()
Dim strLine As String
Dim iLine As Integer
iLine = 0
iFile = FreeFile
Open "C:\MyText.txt" For Input As iFile
Do While Not EOF(iFile)
Line Input #iFile, strLine$
iLine = iLine + 1
If iLine = 50 Then MsgBox strLine$: Exit Sub
Loop
Close #iFile
End Sub
what i really need is to get a specific line in a text file and let it equal a varible. example: i need line 5 to go into CNsme snf line 10 to go into gender ( cname and gender being varibles)
Here's what you can do, open in binary and read the whole file into a buffer, then split it into lines and read any line into any variable anytime:
Code:Dim buffer as string, lines() as string
Open File for binary as 1
buffer=space(lof(1))
get#1,,buffer
lines=split(buffer,vbcrlf)
CNsme = lines(4)
gender = lines(9)
close 1