Home > How-To Library > Text File Handling
Read specific line from text file
**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * **************************************************************** Public Function ReadLine(fName As String, LineNumber As Long) As String 'Parameters: fName = FullPath to File ' LineNumber = LineToRead 'Returns: Contents of the line, or a blank string if ' LineNumber is greater than the total number of lines 'Requires: Reference to Microsoft Scripting Runtime 'Example: MsgBox ReadLine("C:\AutoExec.bat", 3) ' Displays third line of autoexec.bat Dim oFSO As New FileSystemObject Dim oFSTR As Scripting.TextStream Dim ret As Long Dim lCtr As Long If oFSO.FileExists(fName) Then Set oFSTR = oFSO.OpenTextFile(fName) Do While Not oFSTR.AtEndOfStream lCtr = lCtr + 1 If lCtr = LineNumber Then ReadLine = oFSTR.ReadLine Exit Do End If oFSTR.SkipLine Loop oFSTR.Close Set oFSTR = Nothing End If End Function
If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.