?
Solved

count number of lines in a file

Posted on 2003-03-20
6
Medium Priority
?
135 Views
Last Modified: 2010-05-01
hi everyone, is it possible to count the number of lines in a file?
I'm using:

    Dim strTemp As String * 1
    Dim i As Integer
    i = 0
    Open App.Path & "\aras.pol" For Input As 1#
    Do Until EOF(1)
    i% = i% + 1
    Line Input #1, strTemp
    Loop
    Close #1

msgbox "number of lines: " & i

this works but is there another way?
0
Comment
Question by:igfp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:igfp
ID: 8176496
oh, and also to go to a specific line. I'm using the same thing but when i= "number of the line I want" then it exits the cycle. Is there another way to do this as well?
0
 
LVL 13

Accepted Solution

by:
stefri earned 80 total points
ID: 8176710
You can create a file system object
Open the file
Read all lines in a string
use split to cut the string at every vbcr or vblf or vbcrlf
the number of lines will be the upperbound of the array -1
Going to line N would be array(N-1)

This code has been tested in Excel VBA

Option Explicit

Function numLines (theFile) As Long

  Const ForReading = 1, ForWriting = 2
  Dim fso As Object, f As Object
  Dim allLines As String
  Dim linesSplit() As String

  Set fso = CreateObject("Scripting.FileSystemObject")

  Set f = fso.OpenTextFile(theFile, ForReading)
  allLines = f.ReadAll

  linesSplit = Split(allLines, vbCrLf)
  numLines = UBound(linesSplit) - 1
  f.Close
  Set f = Nothing
  Set fso = Nothing
End Function

Sub test()
Dim n As Long

n = numLines ("c:\setuplog.txt")

End Sub

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 8177097
Similar to stefri and yours, you can read the entire file in one shot, then "count" the number of newline characters.

dim strTemp as strnig
dim strLinesSplit() as string
dim iNumLines as integer

' read the entire file
Open App.Path & "\aras.pol" For Input As 1#
strTemp = input$(lof(1), #1)
Close #1

' count the number of newlines, then add one for the last line
strLinesSplit = Split(strTemp, vbNewLine)
iNumLines = UBound(linesSplit) - 1

--
The key here is the Input$ and the Split functions.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:igfp
ID: 8184393
my question now is, since i like mine the best, wich one takes the points????
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 8184425
>is there another way
Since this Q was answered in the first comment (stefri), I think you should accept that as your answer.
0
 

Author Comment

by:igfp
ID: 8184462
as you wish! ;) Thank you both!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 8 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question