The code below counts the number of lines in a file. Simply call the CountLines procedure, passing the file name in its parameter
Function CountLines(ByVal strFilePath As String) As Integer
' 'delcare
variables
Dim fileFile As Integer
Dim intLinesReadCount As Integer
intLinesReadCount = 0
' 'open file
fileFile = FreeFile
If (File_Exists(strFilePath)) Then
Open strFilePath For Input As fileFile
Else
'file doesn't exist
MsgBox "File: " & strFilePath & " hasn't been downloaded
yet. Preprocessing is being aborted.", MB_OK, "File Does Not
Exist"
Count_Lines_In_File = -1
Exit Function
End If
'loop
through file
Dim strBuffer As String
Do While Not EOF(fileFile)
'read line
Input #fileFile, strBuffer
'update count
intLinesReadCount = intLinesReadCount + 1
Loop
'close
file
Close fileFile
'return
value
Count_Lines_In_File = intLinesReadCount
End Function
Function File_Exists(strFilePath As String)
If Dir(strFilePath, vbNormal + vbHidden + vbSystem +
vbReadOnly) = "" Then
File_Exists = False
Else
File_Exists = True
End If
End Function
Comments