Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 29th, 2007, 11:25 AM
Authorized User
 
Join Date: Dec 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default find number of lines in .txt file

hello,
I have a file C:\test\test.txt that I'd like to, using a Function where you pass in the Directory and Filename that you want to utilize, in this case C:\test and test.txt, calculate the number of lines in the text file and return that value from the Function.
Could someone share with me the code that would do this?

ex. if test.txt looked like

lskdjflskjf
lksdflksdj
slkdjf
lksjdf

the Function would return the value 4.

thanks in advance,
david


 
Old January 29th, 2007, 10:48 PM
Registered User
 
Join Date: Jan 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

anyone solve this problem yet. it would be nice to know

 
Old January 30th, 2007, 08:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

can you just open the file and read line by line???

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
 
Old February 2nd, 2007, 03:09 PM
Registered User
 
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

David, do you want to count physical lines (i.e. a long line that wraps counts as two lines) or logical lines (i.e. a long line that wraps counts as one line)?

If you want to count physical lines, try this:

Code:
Private Function CountLines() As Integer
    Dim myLines() As String

    myLines = Split(Text1.Text, vbCrLf)
    CountLines = UBound(myLines) + 1
End Function
 
Old February 2nd, 2007, 03:17 PM
Authorized User
 
Join Date: Dec 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

BBcasey,

thanks for the code, it looks helpful......now what would the code be to open the Text1 file from
the hard drive to then use in the code you supplied?

thanks in advance,
david

 
Old February 5th, 2007, 05:50 PM
Authorized User
 
Join Date: Dec 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

can anyone help me with my question?

thanks,
david

 
Old February 5th, 2007, 06:10 PM
Authorized User
 
Join Date: Dec 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

just found some help on another VB forum....

Private Function LineCount(FileName As String) As Long
Open FileName For Input As #1
Do While Not EOF(1)
I = I + 1
Line Input #1, D
Loop
Close #1
LineCount = I
End Function

Private Sub Command1_Click()
MsgBox LineCount(Environ$("WinDir") & "\Win.INI")
End Sub


 
Old February 29th, 2012, 10:38 PM
Registered User
 
Join Date: Feb 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Talking Very Easy way to find number of lines in a text file

Open text file. Click on Edit>Go To. Give a huge number than expected number of lines. say if you expect to have 2000 lines, give 5000 or just give 200000. It gives an eror "The Line number is beyond the total number of lines" and shows up the the immediately available line. If you have 2379 lines it gives 2380. So whatever it shows up minus 1 is the actual number of lines in the text file. Cheers!!
 
Old July 16th, 2014, 05:11 AM
Registered User
 
Join Date: Jul 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default find number of lines in .txt file

I think TextFinding.com may help you! It is a useful tool to find multiple text in all types of files.





Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList and number of lines snufse1 ASP.NET 2.0 Basics 0 September 19th, 2008 02:52 PM
Need to Count number of lines in a file lawsoncobol Access VBA 2 July 19th, 2007 04:28 AM
Count the number of output lines joyces XSLT 7 July 6th, 2005 11:49 AM
Paged Invoices - Dynamic Number Detail Lines Marga XSLT 1 May 10th, 2005 09:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.